Header Ad

HackerRank Cut the sticks problem solution

In this HackerRank Cut the sticks problem you have Given the lengths of n sticks, print the number of sticks that are left before each iteration until there are none left.

HackerRank Cut the sticks problem solution


Problem solution in Python programming.

numSticks = int(input())
s = [int(i) for i in input().split()]                                                                                 
# s = [5, 4, 4, 2, 2, 8]
s.sort(reverse=True)
while s:
    print(len(s))
    min = s.pop()
    while s and min == s[-1]:
        s.pop()


Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner input = new Scanner(System.in);
        int count = input.nextInt();
        int[] sticks = new int[count];
        int in = 0;
        while(in < count) {
            sticks[in] = input.nextInt();
            in++;
        }
        while(true) {
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < count; i++) {
                if (sticks[i] < min && sticks[i] != 0) {
                    min = sticks[i];
                }
            }
            // System.out.println("Min is " + min);
            int slices = 0;
            for (int i = 0; i < count; i++)  {
                if (sticks[i] > 0) {
                    int temp = sticks[i];
                    sticks[i] = temp - min;
                    // System.out.println("loc " + i + ": " + temp + " to " + sticks[i]);
                    slices++;
                }

            }
            if (slices > 0) 
                System.out.printf("%d%n", slices);
            else
                break;
        }

    }
}


Problem solution in C++ programming.

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define mp make_pair
#define pb push_back
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define F first
#define S second
#define ll long long
#define pp pair<int,int>
#define P 1000000007ll
using namespace std;
const int n_max=300005;
int n,m,i,j,x,k,ans,cur;
vector<int> v;



main()
{scanf("%d",&n);
 for(i=1;i<=n;i++){
    scanf("%d",&x);
    v.pb(x);
 }
 sort(v.begin(),v.end());

 k=0;
 while(k<v.size()){
    ans=(int)v.size()-k;
    cur+=(v[k]-cur);


    while(k<v.size() && v[k]-cur<=0)k++;

    printf("%d\n",ans);
   // system("pause");
 }
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int n;
    int i;
    int q;
    int max=-1;
    scanf("%d", &n);
    int stick[1000];
    stick[0]=0;    
    for(i=0;i<1000;i++)
        stick[i]=0;
    for(i=0;i<n;i++)
    {
        scanf("%d", &q);
        if(q>max)
           max = q;
        stick[q]++;     
    }
    int numcut=0;
    
       		printf("%d\n", n);
    for(i=1;i<max;i++)
    {
        
       if(stick[i])
       {
           numcut+=stick[i];
       		printf("%d\n", n - numcut);
    	}
    }
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    function num(x) { return +x;}
    var sticks = input.split('\n')[1].split(' ').map(num);
    function min(x) { return Math.min.apply(null, x); }
    function dec(x, min) { return x.map(function(y) { return y-min; }).filter(num); }
    var swap = sticks;
    console.log(sticks.length);
    while ((swap = dec(swap, min(swap))) && swap.length) {
        console.log(swap.length);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments