Manasa and Stones Hackerrank problem solution

Manasa and Stones Hackerrank

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 two consecutive stones have a difference of either a or b. Legend has it that there is a treasure trove at the end of the trail and if Manasa can guess the value of the last stone, the treasure would be hers. Given that the number on the first stone was 0, find all the possible values for the number on the last stone.

Note: The numbers on the stones are in increasing order.

Input Format

The first line contains an integer T, i.e. the number of test cases. test cases follow; each has 3 lines. The first line contains n (the number of stones). The second line contains a, and the third line contains b.

Constraints
1<=T<=10 1<=n,a,b<=103
Output Format

Space-separated list of numbers which are the possible values of the last stone in increasing order.

Sample Input

2
3
1
2
4
10
100
Sample Output

2 3 4
30 120 210 300
Explanation

All possible series for the first test case are given below:

0,1,2
0,1,3
0,2,3
0,2,4
Hence the answer 2 3 4.

Series with different number of final steps for second test case are the following:

0, 10, 20, 30
0, 10, 20, 120
0, 10, 110, 120
0, 10, 110, 210
0, 100, 110, 120
0, 100, 110, 210
0, 100, 200, 210
0, 100, 200, 300
Hence the answer 30 120 210 300.

Solution

Manasa and Stones Hackerrank

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
         Scanner in=new Scanner(System.in);
    int t=in.nextInt();
    for(int x=0;x<t;x++){
        int n=in.nextInt();
        int a=in.nextInt();
        int b=in.nextInt();
        if(a==b)System.out.print((n-1)*a);
        else {
            if(a>b){
            int temp=a;
            a=b;
            b=temp;
        }
        for(int i=0;i<n;i++)System.out.print(i*b+(n-1-i)*a+" ");
        }
        System.out.println();
    }
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}
#include<bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    while(t--) {
        long long a, b, n, i;
        long long int c,d;
        set <int> x;
        cin >> n >> c >> d;
        a = min(c,d);
        b = max(c,d);
        n--;
        for (i = 0; i <= n; i++) {
            x.insert(i * b + (n - i) * a);
        }
        for (auto it:x) {
            cout << it << " ";
        }
        cout << endl;
    }
    return 0;
}
for _ in range(input()):
    s = set()
    n = input()
    a = input()
    b = input()
    for i in range(n):
        s.add(i * a + (n - i - 1) * b)
    s = list(s)
    s = sorted(s)
    s = map(str, s)
    print ' '.join(s)

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.