HackerRank 'Manasa and Stones' Solution

by Srikant Padala on April 19, 2016, 7:49 am


Explanation


The best Data Structure to use is Set since it removes the duplicates automatically and keep the elements in it internally sorted.

We have to find all possible permutations of a's and b's of length n-1, since the last element is obtained by adding n-1 a's and/or b's.


Manasa and Stones Problem Statement

Video

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

typedef long long ll;
set<ll> ans;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    ll n,a,b;
    int T;
    cin >> T;
    while(T--) {
        cin >> n >> a >> b;
        ans.clear();
        for(int i = n -1, j = 0 ; i >=0, j < n; i--, j++)
        {
            ans.insert(a*i+b*j);    
        }
        for(set<ll>::iterator it=ans.begin();it != ans.end(); it++) cout << *it << " ";
        cout << endl;
    }
    return 0;
}

Coming Soon.