HackerRank 'Priyanka and Toys' Solution

by Srikant Padala on June 9, 2016, 12:46 am


Explanation


  • Sort the weights in increasing order.

Priyanka and Toys Problem Statement

Video

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

int getMinimumUnits(vector<int> &w) {
    sort(w.begin(), w.end());
    int minUnits = 1, i=0, ans=1;
    for(int j=1; j < w.size(); j++) {
        while(w[i] + 4 >= w[j]) j++;
        i=j;
        if(i < w.size()) ans++;
    }
    return ans;
}
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    cin >> n;
    vector<int> w(n);
    for(int i=0; i < n; i++) {
        cin >> w[i];
    }
    cout << getMinimumUnits(w);
    return 0;
}

Coming Soon.