HackerRank Cut the sticks Problem Solution Yashwant Parihar, April 16, 2023April 18, 2023 In this post, we will solve HackerRank Cut the sticks Problem Solution.You are given a number of sticks of varying lengths. You will iteratively cut the sticks into smaller sticks, discarding the shortest pieces until there are none left. At each iteration you will determine the length of the shortest stick remaining, cut that length from each of the longer sticks and then discard all the pieces of that shortest length. When all the remaining sticks are the same length, they cannot be shortened so discard them.Given the lengths of n sticks, print the number of sticks that are left before each iteration until there are none left.Examplearr = [1, 2, 3]The shortest stick length is 1, so cut that length from the longer two and discard the pieces of length 1. Now the lengths are arr = [1, 2]. Again, the shortest stick is of length 1, so cut that amount from the longer stick and discard those pieces. There is only one stick left, arr = [1], so discard that stick. The number of sticks at each iteration are answer = [3, 2, 1].Function DescriptionComplete the cutTheSticks function in the editor below. It should return an array of integers representing the number of sticks before each cut operation is performed.cutTheSticks has the following parameter(s):int arr[n]: the lengths of each stickReturnsint[]: the number of sticks after each iterationInput FormatThe first line contains a single integer n, the size of arr.The next line contains n space-separated integers, each an arr[i], where each valuerepresents the length of the ith stick.Sample Input 0STDIN Function ----- -------- 6 arr[] size n = 6 5 4 4 2 2 8 arr = [5, 4, 4, 2, 2, 8] Sample Output 06 4 2 1 Explanation 0sticks-length length-of-cut sticks-cut 5 4 4 2 2 8 2 6 3 2 2 _ _ 6 2 4 1 _ _ _ _ 4 1 2 _ _ _ _ _ 3 3 1 _ _ _ _ _ _ DONE DONE Sample Input 18 1 2 3 4 3 3 2 1 Sample Output 18 6 4 1 Explanation 1sticks-length length-of-cut sticks-cut 1 2 3 4 3 3 2 1 1 8 _ 1 2 3 2 2 1 _ 1 6 _ _ 1 2 1 1 _ _ 1 4 _ _ _ 1 _ _ _ _ 1 1 _ _ _ _ _ _ _ _ DONE DONEHackerRank Cut the sticks Problem SolutionCut the sticks C Solution#include <stdio.h> #include <stdlib.h> int cmp (const void *a, const void *b) { return (*(int*) a - *(int*) b); } int main() { // Get the input int n=0, N=0; scanf("%d", &N); int *a = (int *) calloc(N, sizeof (int)); for (n=0;n<N;n++) scanf("%d", &a[n]); // Sort the input qsort(a, N, sizeof (int), cmp); int start=0; while (start<N) { int b = a[start]; printf("%d\r\n", N-start); for (n=start;n<N;n++) { a[n] -= b; if (a[n]==0) start++; } } return 0; }Cut the sticks C++ Solution#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int arr[10001],n,i,j,m,k; cin>>n; int min = 10002; for(i=0;i<n;i++){ cin>>arr[i]; if(min>arr[i])min=arr[i]; } int cnt = 0; int temp = min; while(cnt!=n){ int flag = 0,ans = 0; min = 10002; for(i=0;i<n;i++){ if(arr[i]==0){ flag++; continue; } else{ ans++; arr[i]-=temp; if(arr[i]==0)flag++; else{ if(arr[i]<min)min = arr[i]; } } } temp = min; cnt = flag; cout<<ans<<endl; // cout<<cnt<<endl; } return 0; }Cut the sticks C Sharp Solutionusing System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { var N = int.Parse(Console.ReadLine()); var s = Console.ReadLine().Split(' '); int[] a = new int[N]; for (int i = 0; i < s.Count(); i++) { a[i] = int.Parse(s[i]); } cek: if (a.Max() != 0) { int c = 0; Array.Sort(a); int min = 0; for (int q = 0; q < a.Count(); q++) { min = a[q] != 0 ? a[q] : a[q + 1]; if (min != 0) break; } for (int i = 0; i < a.Count(); i++) { if (a[i] > 0) { a[i] = a[i] - min; c++; } } Console.WriteLine(c); goto cek; } } }Cut the sticks Java Solutionimport java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'cutTheSticks' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts INTEGER_ARRAY arr as parameter. */ public static List<Integer> cutTheSticks(List<Integer> arr) { // Write your code here List<Integer> sticksCut = new ArrayList<Integer>(); List<Integer> remaining; Collections.sort(arr); while (arr.size() > 0) { remaining = new ArrayList<Integer>(); int smallestCut = arr.get(0); sticksCut.add(arr.size()); for (int i = 0; i < arr.size(); i++) { if (arr.get(i) - smallestCut > 0) { remaining.add(arr.get(i) - smallestCut); } } arr = remaining; } return sticksCut; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int n = Integer.parseInt(bufferedReader.readLine().trim()); List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()); List<Integer> result = Result.cutTheSticks(arr); bufferedWriter.write( result.stream() .map(Object::toString) .collect(joining("\n")) + "\n" ); bufferedReader.close(); bufferedWriter.close(); } }Cut the sticks JavaScript Solutionfunction processData(input) { var length = input.split('\n')[0]; var arr = input.split('\n')[1].split(' '); var min, newArr=[], i, count, curL; while(length){ min = arr[0]; count = 0; for(i=0; i<length; i++){ if(parseInt(arr[i])<parseInt(min)){ min = arr[i]; } }; curL = length; for(i=0; i<curL; i++){ arr[i] = arr[i]-min; count++; if(arr[i]){ newArr.push(arr[i]); } else { length--; } } console.log(count); arr = newArr; newArr = []; } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });Cut the sticks Python Solutiondef run(): input() # discard number sticks = list(map(int, input().split(" "))) while len(sticks) > 0: print(len(sticks)) m = min(sticks) sticks = [s - m for s in sticks if s - m > 0] run()Other solutionsHackerRank Non-Divisible Subset Problem SolutionHackerRank Repeated String Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython