HackerRank 'Cut the Sticks' Solution

by Srikant Padala on June 25, 2016, 10:15 am


Explanation


Keep remving the smallest size stick(s) from the array and print its size before removing. 


Cut the Sticks Problem Statement

Video

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

int main(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int arr_i = 0;arr_i < n;arr_i++){
       cin >> arr[arr_i];
    }
    while(arr.size() > 0) {
        cout << arr.size() << endl;
        int min = *min_element(arr.begin(), arr.end());
        for(vector<int>::iterator itr = arr.begin(); itr != arr.end();) {
            if(*itr == min) arr.erase(itr);
            else itr++;
        }
    }
    return 0;
}

Coming Soon.