Cut the sticks

Parag Naik
2 min readMar 4, 2021


Problem:-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.

Photo by Nathan Dumlao on Unsplash

An interesting hackerrank problem, where we return the count of sticks after every time we reduce the size of sticks and eliminate them once its length becomes 0.Lets move towards the code, we are passing list “arr” which holds the length of all sticks.we will first declare 1 empty list count which will hold the number of sticks present when the iteration ends.We will use a while loop which will run till the length of the arr becomes 0.Inside the while loop we will substract each element of list with the minimum value in the list “arr” and check if this subtraction is not zero and update the list “arr”,here we are using list comprehension and then we will append the number of sticks present after discarding zero length sticks at the start of while loop and the function will return count list.

def cutTheSticks(arr):
while (len(arr)!=0):
arr=[x-min(arr) for x in arr if (x-min(arr)!=0)]
return count

All we have to decide is what to do with the time that is given us.
Time is precious and cannot be wasted under any circumstances, find challenge or thing which scares you that’s when the magic will happen.Keep Learning and keep husting…..(Cheers:-PN)