Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Cut the sticks problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Cut the sticks problem solution 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

HackerRank Cut the Sticks Problem solution in Python.

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()

Cut the Sticks 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 AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes