HackerRank Manasa and Stones problem solution YASH PAL, 31 July 2024 In this HackerRank Manasa and Stones problem you need to Compute all possible numbers that might occur on the last stone given a starting stone with a 0 on it, the number of additional stones found, and the possible differences between consecutive stones. Order the list ascending. Problem solution in Python programming. #!/usr/bin/env python import sys if __name__ == '__main__': T = int(sys.stdin.readline()) for _ in range(T): n = int(sys.stdin.readline()) a = int(sys.stdin.readline()) b = int(sys.stdin.readline()) print(*sorted(set(x * a + (n - 1 - x) * b for x in range(n)))) Problem solution in Java Programming. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int numberOfTestCases = scan.nextInt(); for (int i = 0; i < numberOfTestCases; ++i) { int numberOfStones = scan.nextInt() - 1; int a = scan.nextInt(); int b = scan.nextInt(); if (numberOfStones <= 0) { System.out.println(0); } else { Vector<Integer> results = new Vector<Integer>(); for (int j = 0; j <= numberOfStones; ++j) { results.add((numberOfStones - j) * a + j * b); } Collections.sort(results); System.out.print(results.elementAt(0)); for (int j = 1; j < results.size(); ++j) { if (results.elementAt(j).equals(results.elementAt(j - 1))) { continue; } else { System.out.print(" " + results.elementAt(j)); } } System.out.println(); } } } } Problem solution in C++ programming. #include <iostream> #include <set> using namespace std; int main() { int nTests = 0; cin >> nTests; while (nTests--) { int n = 0; int a = 0; int b = 0; cin >> n >> a >> b; set<int> s; for (int i = 0; i <= n - 1; ++i) { s.insert(i * a + (n - 1 - i) * b); } for (set<int>::iterator it = s.begin(); it != s.end(); ++it) { if (it != s.begin()) cout << " "; cout << *it; } cout << "n"; } return 0; } Problem solution in C programming. #include <stdio.h> #include <stdlib.h> typedef struct path { long int numSteps; long int a; long int b; } path; int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main() { int numCases, i, j; path p; scanf("%d", &numCases); for (i = 0; i < numCases; i++) { long int firstStone = 0; scanf("%ld", &p.numSteps); scanf("%ld", &p.a); scanf("%ld", &p.b); int possibleLastStones[p.numSteps]; if(p.a == p.b) { printf("%dn", p.a*(p.numSteps-1)); } else { for(j = 0; j < p.numSteps; j++) { possibleLastStones[j] = firstStone + j*p.b + (p.numSteps - 1 - j)*p.a; } //sort possibleLastStones array qsort(possibleLastStones, p.numSteps, sizeof(int), compare); //print out sorted array for(j = 0; j < p.numSteps; j++) { printf("%d ", possibleLastStones[j]); } printf("n"); } } return 0; } Problem solution in JavaScript programming. 'use strict'; function processData(input) { var parse_fun = function (s) { return parseInt(s, 10); }; var answer = ''; var lines = input.split('n'); lines.splice(0,1); while(lines.length > 2){ var stones = parseInt(lines[0]); var a = Math.min(parseInt(lines[1]), parseInt(lines[2])); var b = Math.max(parseInt(lines[2]), parseInt(lines[1])); var search = {}; var paths = {level1:[0]}; for(var i = 2;i<=stones;i++) { paths['level'+i] = []; for(var j = 0; j < paths['level'+(i-1)].length;j++){ if(paths['level'+i].indexOf(parseInt(paths['level'+(i-1)][j])+a) == -1) paths['level'+i].push(parseInt(paths['level'+(i-1)][j])+a); if(paths['level'+i].indexOf(parseInt(paths['level'+(i-1)][j])+b) == -1) paths['level'+i].push(parseInt(paths['level'+(i-1)][j])+b); } } answer += paths['level'+stones].join(" ")+"n"; lines.splice(0,3); } process.stdout.write(answer); } process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); algorithm coding problems