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.

Ezoicreport this adHackerRank Manasa and Stones Solution
HackerRank Manasa and Stones Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Manasa and Stones Solution

Ezoicreport this adTask

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

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.

Constraints

  • 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

Explanation

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) {
          x=a;
          a=b;
          b=x;
        }
        n--;
        base=n*a;
        if(a!=b) {
          while(n>0) {
            printf("%ld ", base);
            n--;
            base+=b-a;
          }
        }
        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 + " ");
                    used.add(sum);
                }
                sum -= min;
                sum += max;
            }
            if (used.contains(sum))
                System.out.println();
            else
                System.out.println(sum);
        }
        in.close();
    }
}
Ezoicreport this ad

Manasa and Stones Solution in Python

T=int(raw_input())
for _ in range(0,T):
    n=int(raw_input())
    a=int(raw_input())
    b=int(raw_input())
    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.push(i);
        }
        res.sort(function(n1, n2) { return n1 - n2; });
        console.log(res.join(' '));
    }
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
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;
begin
 readln(t);
 for i:=1 to t do
 begin
  readln(n);
  readln(a);
  readln(b);
  if a>b then
   begin
    q:=a;
    a:=b;
    b:=q;
   end;
  k1:=n-1;
  k2:=0;
  if a=b then write(a*(n-1),' ') else
  while k2<n do
  begin
   write(a*k1+b*k2,' ');
   k1:=k1-1;
   k2:=k2+1;
  end;
  writeln;
 end;
end.

Disclaimer: This problem (Manasa and Stones) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Happy Ladybugs Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad