Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Manasa and Stones problem solution

YASH PAL, 31 July 2024

In this HackerRank Manasa and Stones problem you need to Compute all possible numbers that might occur on the last stone given a starting stone with a 0 on it, the number of additional stones found, and the possible differences between consecutive stones. Order the list ascending.

HackerRank Manasa and Stones problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

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))))

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes