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

**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(n^{2}) time.

**Efficient solution ** works in O(nlog(n)). First we have to sort all Ropes in increasing order of there 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 notIF ( 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.

## C++

`// C++ program to print how many ` `// Ropes are Left After Every Cut ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function print how many Ropes are ` `// Left AfterEvery Cutting operation ` `void` `cuttringRopes(` `int` `Ropes[], ` `int` `n) ` `{ ` ` ` `// sort all Ropes in increase ` ` ` `// of there length ` ` ` `sort(Ropes, Ropes + n); ` ` ` ` ` `int` `singleOperation = 0; ` ` ` ` ` `// min length rope ` ` ` `int` `cuttingLenght = Ropes[0]; ` ` ` ` ` `// now traverse through the given ` ` ` `// Ropes in increase order of length ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `// After cutting if current rope length ` ` ` `// is greater than '0' that mean all ` ` ` `// ropes to it's right side are also ` ` ` `// greater than 0 ` ` ` `if` `(Ropes[i] - cuttingLenght > 0) ` ` ` `{ ` ` ` `// print number of ropes remains ` ` ` `cout << (n - i) << ` `" "` `; ` ` ` ` ` `// now current rope become ` ` ` `// min length rope ` ` ` `cuttingLenght = Ropes[i]; ` ` ` `singleOperation++; ` ` ` `} ` ` ` `} ` ` ` `if` `(singleOperation == 0) ` ` ` `cout << ` `"0 "` `; ` `} ` `int` `main() ` `{ ` ` ` `int` `Ropes[] = { 5, 1, 1, 2, 3, 5 }; ` ` ` `int` `n = ` `sizeof` `(Ropes) / ` `sizeof` `(Ropes[0]); ` ` ` `cuttringRopes(Ropes, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print how many ` `// Ropes are Left After Every Cut ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function print how many Ropes are Left After ` ` ` `// Every Cutting operation ` ` ` `public` `static` `void` `cuttringRopes(` `int` `Ropes[], ` `int` `n) ` ` ` `{ ` ` ` `// sort all Ropes in increasing ` ` ` `// order of their length ` ` ` `Arrays.sort(Ropes); ` ` ` ` ` `int` `singleOperation = ` `0` `; ` ` ` ` ` `// min length rope ` ` ` `int` `cuttingLenght = Ropes[` `0` `]; ` ` ` ` ` `// now traverse through the given Ropes in ` ` ` `// increase order of length ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) ` ` ` `{ ` ` ` `// After cutting if current rope length ` ` ` `// is greater than '0' that mean all ` ` ` `// ropes to it's right side are also ` ` ` `// greater than 0 ` ` ` `if` `(Ropes[i] - cuttingLenght > ` `0` `) ` ` ` `{ ` ` ` `System.out.print(n - i + ` `" "` `); ` ` ` ` ` `// now current rope become ` ` ` `// min length rope ` ` ` `cuttingLenght = Ropes[i]; ` ` ` ` ` `singleOperation++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// after first operation all ropes ` ` ` `// length become zero ` ` ` `if` `(singleOperation == ` `0` `) ` ` ` `System.out.print(` `"0"` `); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] arg) ` ` ` `{ ` ` ` `int` `[] Ropes = { ` `5` `, ` `1` `, ` `1` `, ` `2` `, ` `3` `, ` `5` `}; ` ` ` `int` `n = Ropes.length; ` ` ` `cuttringRopes(Ropes, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to ` `# print how many ` `# Ropes are Left After ` `# Every Cut ` ` ` `# Function print how many Ropes are ` `# Left AfterEvery Cutting operation ` `def` `cuttringRopes(Ropes, n) : ` ` ` ` ` `# sort all Ropes in increase ` ` ` `# of there length ` ` ` `Ropes.sort() ` ` ` ` ` `singleOperation ` `=` `0` ` ` ` ` `# min length rope ` ` ` `cuttingLenght ` `=` `Ropes[` `0` `] ` ` ` ` ` `# now traverse through the given ` ` ` `# Ropes in increase order of length ` ` ` `for` `i ` `in` `range` `(` `1` `,n) : ` ` ` ` ` `# After cutting if current rope length ` ` ` `# is greater than '0' that mean all ` ` ` `# ropes to it's right side are also ` ` ` `# greater than 0 ` ` ` `if` `(Ropes[i] ` `-` `cuttingLenght > ` `0` `) : ` ` ` ` ` `# print number of ropes remains ` ` ` `print` `((n ` `-` `i) ,end` `=` `" "` `) ` ` ` ` ` `# now current rope become ` ` ` `# min length rope ` ` ` `cuttingLenght ` `=` `Ropes[i] ` ` ` `singleOperation ` `=` `singleOperation ` `+` `1` ` ` ` ` ` ` `if` `(singleOperation ` `=` `=` `0` `) : ` ` ` `print` `(` `"0 "` `,end` `=` `"") ` ` ` ` ` `Ropes ` `=` `[ ` `5` `, ` `1` `, ` `1` `, ` `2` `, ` `3` `, ` `5` `] ` `n ` `=` `len` `(Ropes) ` `cuttringRopes(Ropes, n) ` ` ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print how many ` `// Ropes are Left After Every Cut ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function print how many Ropes are Left After ` ` ` `// Every Cutting operation ` ` ` `public` `static` `void` `cuttringRopes(` `int` `[]Ropes, ` `int` `n) ` ` ` `{ ` ` ` `// sort all Ropes in increasing ` ` ` `// order of their length ` ` ` `Array.Sort(Ropes); ` ` ` ` ` `int` `singleOperation = 0; ` ` ` ` ` `// min length rope ` ` ` `int` `cuttingLenght = Ropes[0]; ` ` ` ` ` `// now traverse through the given Ropes in ` ` ` `// increase order of length ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `// After cutting if current rope length ` ` ` `// is greater than '0' that mean all ` ` ` `// ropes to it's right side are also ` ` ` `// greater than 0 ` ` ` `if` `(Ropes[i] - cuttingLenght > 0) ` ` ` `{ ` ` ` `Console.Write(n - i + ` `" "` `); ` ` ` ` ` `// now current rope become ` ` ` `// min length rope ` ` ` `cuttingLenght = Ropes[i]; ` ` ` ` ` `singleOperation++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// after first operation all ropes ` ` ` `// length become zero ` ` ` `if` `(singleOperation == 0) ` ` ` `Console.Write(` `"0"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] Ropes = { 5, 1, 1, 2, 3, 5 }; ` ` ` `int` `n = Ropes.Length; ` ` ` `cuttringRopes(Ropes, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## php

`<?php ` `// PHP program to print how many ` `// Ropes are Left After Every Cut ` ` ` `// Function print how many Ropes are ` `// Left AfterEvery Cutting operation ` `function` `cuttringRopes(` `$Ropes` `, ` `$n` `) ` `{ ` ` ` ` ` `// sort all Ropes in increase ` ` ` `// of there length ` ` ` `sort(` `$Ropes` `); ` ` ` ` ` `$singleOperation` `= 0; ` ` ` ` ` `// min length rope ` ` ` `$cuttingLenght` `= ` `$Ropes` `[0]; ` ` ` ` ` `// now traverse through the given ` ` ` `// Ropes in increase order of length ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// After cutting if current rope length ` ` ` `// is greater than '0' that mean all ` ` ` `// ropes to it's right side are also ` ` ` `// greater than 0 ` ` ` `if` `(` `$Ropes` `[` `$i` `] - ` `$cuttingLenght` `> 0) ` ` ` `{ ` ` ` `// print number of ropes remains ` ` ` `echo` `(` `$n` `- ` `$i` `). ` `" "` `; ` ` ` ` ` `// now current rope become ` ` ` `// min length rope ` ` ` `$cuttingLenght` `= ` `$Ropes` `[` `$i` `]; ` ` ` `$singleOperation` `++; ` ` ` `} ` ` ` `} ` ` ` `if` `(` `$singleOperation` `== 0) ` ` ` `echo` `"0 "` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$Ropes` `= ` `array` `(5, 1, 1, 2, 3, 5); ` ` ` `$n` `= ` `count` `(` `$Ropes` `); ` ` ` `cuttringRopes(` `$Ropes` `, ` `$n` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

4 3 2

Time Complexity : O(n long (n))

Space complexity : O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum removal from array when removal time >= waiting time
- Maximize sum of remaining elements after every removal of the array half with greater sum
- N consecutive ropes problem
- Replace every element with the smallest element on its left side
- Minimum peak elements from an array by their repeated removal at every iteration of the array
- Minimize the maximum minimum difference after one removal from array
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Longest palindromic string possible after removal of a substring
- Replace every element with the greatest element on its left side
- Closest greater or same value on left side for every element in array
- Largest element smaller than current element on left for every element in Array
- Count greater elements on the left side of every array element
- Minimum element left from the array after performing given operations
- Find the index of the left pointer after possible moves in the array
- Find the minimum number of rectangles left after inserting one into another
- Find the Mth element of the Array after K left rotations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.