HackerRank Manasa and Stones Problem Solution Yashwant Parihar, April 19, 2023April 19, 2023 In this post, we will solve HackerRank Manasa and Stones Problem Solution.Manasa is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that any two consecutive stones’ numbers differ by one of two values. Legend has it that there is a treasure trove at the end of the trail. If Manasa can guess the value of the last stone, the treasure will be hers.Examplen = 2a = 2b = 3She finds 2 stones and their differences are a = 2 or b = 3. We know she starts with a 0 stone not included in her count. The permutations of differences for the two stones are [2,2], [2, 3], [3,2] or [3, 3]. Looking at each scenario, stones might have [2, 4], [2, 5], [3, 5] or [3, 6] on them. The last stone might have any of 4, 5, or 6 on its face.Compute all possible numbers that might occur on the last stone given a starting stone with a 0 on it, a number of additional stones found, and the possible differences between consecutive stones. Order the list ascending.Function DescriptionComplete the stones function in the editor below.stones has the following parameter(s):int n: the number of non-zero stonesint a: one possible integer differenceint b: another possible integer differenceReturnsint[]: all possible values of the last stone, sorted ascendingInput FormatThe first line contains an integer T, the number of test cases.Each test case contains 3 lines:-The first line contains n, the number of non-zero stones found.-The second line contains a, one possible difference-The third line contains b, the other possible difference.Sample InputSTDIN Function ----- -------- 2 T = 2 (test cases) 3 n = 3 (test case 1) 1 a = 1 2 b = 2 4 n = 4 (test case 2) 10 a = 10 100 b = 100 Sample Output2 3 4 30 120 210 300 ExplanationWith differences 1 and 2, all possible series for the first test case are given below:1.0,1,20,1,30,2,30,2,4Hence the answer 2 3 4.With differences 10 and 100, all possible series for the second test case are the following:0, 10, 20, 302.0, 10, 20, 1203.0, 10, 110, 1204.0, 10, 110, 2105.0, 100, 110, 1206.0, 100, 110, 2107.0, 100, 200, 2108.0, 100, 200, 300Hence the answer 30 120 210 300.HackerRank Manasa and Stones Problem SolutionManasa and Stones C Solution/* * * Values can be obtained as this: * * 0 * a b * 2a a+b 2b * 3a 2a+b a+2b 3b * * and so on, depending on the value of n. */ #include <stdio.h> void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } void values(const int n, int a, int b) { int i; if (a > b) swap(&a, &b); if (a == b) /* there will be just one unique value */ printf("%d", (n - 1) * a); else for (i = 0; i < n; i++) printf("%d ", (n - 1 - i) * a + i * b); printf("\n"); } int main() { int T, i; scanf("%d", &T); for (i = 0; i < T; i++) { int n, a, b; scanf("%d", &n); scanf("%d", &a); scanf("%d", &b); values(n, a, b); } return 0; }Manasa and Stones C++ Solution#include <algorithm> #include <iostream> #include <iterator> #include <set> using namespace std; int main() { int t; cin >> t; while (t-- > 0) { int n, a, b; cin >> n >> a >> b; set<int> values {0}; for (int i = 1; i < n; i++) { set<int> new_values; for (auto v : values) { new_values.insert(v + a); new_values.insert(v + b); } swap(values, new_values); } copy(begin(values), end(values), ostream_iterator<int>(cout, " ")); cout << endl; } }Manasa and Stones C Sharp Solutionusing System; using System.IO; using System.Collections.Generic; using System.Linq; namespace Solution{ public class Program{ public static void Main(){ long T, n, a, b, i; List<long> Now; T = Convert.ToInt32(Console.ReadLine()); for(;T>0;T--){ n = Convert.ToInt32(Console.ReadLine()); a = Convert.ToInt32(Console.ReadLine()); if(a<0)a=-a; b = Convert.ToInt32(Console.ReadLine()); if(b<0)b=-b; if(a == b) { Console.WriteLine((n-1)*a); } else { Now = new List<long>(); for(i=0;i<n;i++) { Now.Add(i*a + (n-i-1)*b); } Now = Now.OrderBy(x => x).ToList(); foreach(var x in Now){ Console.Write(x); Console.Write(" "); } Console.WriteLine(); } } } } }Manasa and Stones Java Solutionimport java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'stones' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts following parameters: * 1. INTEGER n * 2. INTEGER a * 3. INTEGER b */ public static List<Integer> stones(int n, int a, int b) { // Write your code here n--; Set<Integer> result = new TreeSet<>(); int k = n; while (k > -1) { result.add(k * a + (n - k) * b); k--; } return new ArrayList<>(result); } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int T = Integer.parseInt(bufferedReader.readLine().trim()); IntStream.range(0, T).forEach(TItr -> { try { int n = Integer.parseInt(bufferedReader.readLine().trim()); int a = Integer.parseInt(bufferedReader.readLine().trim()); int b = Integer.parseInt(bufferedReader.readLine().trim()); List<Integer> result = Result.stones(n, a, b); bufferedWriter.write( result.stream() .map(Object::toString) .collect(joining(" ")) + "\n" ); } catch (IOException ex) { throw new RuntimeException(ex); } }); bufferedReader.close(); bufferedWriter.close(); } }Manasa and Stones Javascript Solutionfunction processData(input) { var lines = input.split("\n").map(function(s) { return parseInt(s, 10); }), cases = lines.shift(), n, a, b, diff, max, cur, results; for(var i = 0; i < cases; i++) { results = []; n = lines[i*3]-1; a = Math.min(lines[i*3+1], lines[i*3+2]); b = Math.max(lines[i*3+1], lines[i*3+2]); diff = b-a; cur = n*a; max = n*b; if(a == b) { results.push(max); } else { while(cur <= max) { results.push(cur); cur += diff; } } console.log(results.join(" ")); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });Manasa and Stones Python Solutiondef solve(stones, a, b): """ Solves the stones problem recursivly. """ results = set() for i in range(stones): j = stones - 1 - i results.add(i * a + j * b) return results def read_testcase(): """ Reads testcase from user. """ stones = int(input()) a = int(input()) b = int(input()) return stones, a, b def main(): """ Read testcases and solve them. """ testcases = int(input()) tests = [solve(*read_testcase()) for _ in range(testcases)] for results in tests: print(' '.join(str(result) for result in sorted(results))) if __name__ == "__main__": main()Other SolutionsHackerRank The Grid Search Problem SolutionHackerRank Happy Ladybugs Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython