Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Cut the sticks problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

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("%dn",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("%dn", n);
    for(i=1;i<max;i++)
    {
        
       if(stick[i])
       {
           numcut+=stick[i];
       		printf("%dn", 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);
});

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes