Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Manasa and Stones problem solution

YASH PAL, 31 July 202414 December 2025

In this 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.

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
HackerRank Manasa and Stones problem solution

HackerRank Manasa and Stones 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))))

Manasa and Stones 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); });

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes