![HackerRank Manasa and Stones Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Manasa-and-Stones-Problem-Solution.jpg)
HackerRank Manasa and Stones Problem Solution
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.
Example
n = 2
a = 2
b = 3
She 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 Description
Complete the stones function in the editor below.
stones has the following parameter(s):
- int n: the number of non-zero stones
- int a: one possible integer difference
- int b: another possible integer difference
Returns
- int[]: all possible values of the last stone, sorted ascending
Input Format
The 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 Input
STDIN 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 Output
2 3 4
30 120 210 300
Explanation
With differences 1 and 2, all possible series for the first test case are given below:
1.0,1,2
- 0,1,3
- 0,2,3
- 0,2,4
Hence the answer 2 3 4.
With differences 10 and 100, all possible series for the second test case are the following: - 0, 10, 20, 30
2.0, 10, 20, 120
3.0, 10, 110, 120
4.0, 10, 110, 210
5.0, 100, 110, 120
6.0, 100, 110, 210
7.0, 100, 200, 210
8.0, 100, 200, 300
Hence the answer 30 120 210 300.
![HackerRank Manasa and Stones Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Manasa-and-Stones-Problem-Solution-1024x576.jpg)
Manasa 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 Solution
using 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 Solution
import 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 Solution
function 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 Solution
def 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 Solutions