HackerRank Manasa and Stones Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Manasa and Stones Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Manasa and Stones Solution
Manasa and Stones Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Manasa and Stones


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.


n = 2
a = 2
b = 3

She finds  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 45 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


  • 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.


  • 1 <= T <= 10
  • 1 <= nab <= 103

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


With differences 1 and 2, all possible series for the first test case are given below:

  1. 0,1,2
  2. 0,1,3
  3. 0,2,3
  4. 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:

  1. 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 Solution

Manasa and Stones Solution in C

#include <stdio.h>
#include <stdint.h>

int main() {
    uint16_t T;
    scanf("%d", &T);
    for(uint16_t count=1; count<=T; count++) {
        uint64_t n, a, b, x, base;
        scanf("%ld", &n);
        scanf("%ld", &a);
        scanf("%ld", &b);
        if(a>b) {
        if(a!=b) {
          while(n>0) {
            printf("%ld ", base);
        printf("%ld\n", base);
    return 0;

Manasa and Stones Solution in Cpp

#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;

Manasa and Stones Solution in Java

import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int i = 0; i < T; i++) {
            int n = in.nextInt() - 1;
            int a = in.nextInt();
            int b = in.nextInt();
            int min = Math.min(a, b);
            int max = Math.max(a, b);
            int sum = n * min;
            Set<Integer> used = new HashSet<Integer>();
            for (int j = 0; j < n; j++) {
                if (!used.contains(sum)) {
                    System.out.print(sum + " ");
                sum -= min;
                sum += max;
            if (used.contains(sum))

Manasa and Stones Solution in Python

for _ in range(0,T):
    if (a<b): a,b=b,a
    if (a==b): print (n-1)*a
    else: print " ".join(map(str,[i*a+(n-1-i)*b for i in xrange(0,n)]))

Manasa and Stones Solution using JavaScript

'use strict';

function processData(input) {
    var parse_fun = function (s) { return parseInt(s, 10); };

    var lines = input.split('\n');
    var T = parse_fun(lines.shift());

    for (var t = 0; t < T; t++) {
        var n = parse_fun(lines[3 * t]);
        var a = parse_fun(lines[3 * t + 1]);
        var b = parse_fun(lines[3 * t + 2]);
        var s = {};
        for (var i = 0; i < n; i++) {
            s[i * a + (n - 1 - i) * b] = 1;
        var res = [];
        for (var i in s) {
        res.sort(function(n1, n2) { return n1 - n2; });
        console.log(res.join(' '));

var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });

Manasa and Stones Solution in Scala

object Solution {
    def main(arg:Array[String]) {
        (1 to readInt).foreach(_ => {
            def min(x:Int,y:Int) = if (x < y) x else y
            def max(x:Int,y:Int) = if (x > y) x else y
            val n = readInt
            val a = readInt
            val b = readInt
            if (a == b) println((n - 1) * a)
            else {
                val A = min(a, b)
                val B = max(a, b)
                println(((n - 1) * A to (n - 1) * B by B - A).mkString(" "))

Manasa and Stones Solution in Pascal

var n,a,b,t,k1,k2,i,q: integer;
 for i:=1 to t do
  if a>b then
  if a=b then write(a*(n-1),' ') else
  while k2<n do
   write(a*k1+b*k2,' ');

Disclaimer: This problem (Manasa and Stones) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.


1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment