VOOZH about

URL: https://www.geeksforgeeks.org/dsa/ropes-left-every-cut/

⇱ Ropes left after every removal of smallest - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ropes left after every removal of smallest

Last Updated : 9 Nov, 2022

Given an array of an integer of size, N. Array contains N ropes of length Ropes[i]. You have to perform a cut operation on ropes such that all of them are reduced by the length of the smallest rope. Display the number of ropes left after every cut. Perform operations till the length of each rope becomes zero. 

Note: IF no ropes left after a single operation, in this case, we print 0. 

Examples:  

Input : Ropes[] = { 5, 1, 1, 2, 3, 5 } 
Output : 4 3 2 
Explanation : In first operation the minimum ropes is 1 so we reduce length 1 from all of them after reducing we left with 4 ropes and we do same for rest.

Input : Ropes[] = { 5, 1, 6, 9, 8, 11, 2, 2, 6, 5 } 
Output : 9 7 5 3 2 1 

Recommended Practice

Simple solution is to we traverse a loop from [0...n-1], In each iterations first we find min length rope. After that, we reduce all ropes length by it and then count how many ropes are left whose length is greater than zero. this process is done until all ropes length is greater than zero. This solution work in O(n2) time.

Efficient solution works in O(nlog(n)). First we have to sort all Ropes in increasing order of their length. after that we have follow the step.  

//initial cutting length "min rope" 
CuttingLength = Ropes[0]
Now Traverse a loop from left to right [1...n]
 .During traverse we check that 
 is current ropes length is greater than zero or not 
 IF ( Ropes[i] - CuttingLength > 0 ) 
 .... IF Yes then all ropes to it's right side also greater than 0
 .... Print number of ropes remains (n - i)
 ....update Cutting Length by current rope length
 ...... CuttingLength = Ropes[i]  
Do the same process for the rest.


Below is the implementation of above idea. 


Output
4 3 2 

Time Complexity : O(n log (n)) 
Space complexity : O(1)

Comment
Article Tags:
Article Tags: