# Hackerrank Problem – Manasa and Stones

Statement –

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.

For example, assume she finds stones and their differences are or . We know she starts with a stone not included in her count. The permutations of differences for the two stones would be or . Looking at each scenario, stones might have or on them. The last stone might have any of , or on its face.

Compute all possible numbers that might occur on the last stone given a starting stone with a on it, a number of additional stones found, and the possible differences between consecutive stones. Order the list ascending.

For detailed problem statement – https://www.hackerrank.com/challenges/manasa-and-stones/problem

```
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the stones function below.
static int[] stones(int n, int a, int b) {
//int[] temp = new int[n];
if(a==b){
int[] temp = new int[1];
temp[0] = (n-1)*b;
return temp;
}
int[] temp = new int[n];
for(int i=0; i<n; i++){
int temp_v = a*((n-1)-i)+b*i;
temp[i] = temp_v;
}
Arrays.sort(temp);
return temp;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int T = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int TItr = 0; TItr < T; TItr++) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int a = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int b = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] result = stones(n, a, b);
for (int i = 0; i < result.length; i++) {
bufferedWriter.write(String.valueOf(result[i]));
if (i != result.length - 1) {
bufferedWriter.write(" ");
}
}
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}
```