Ropes left after every removal of smallest rope


In this article, we will discuss two approaches to solve the problem - Ropes left after every removal of smallest rope.

Problem Statement

We are given an array of elements where array [i] denotes the length of the ith rope in the array. Our task is to cut a length equal to the smallest element of the array from all the elements of the array until we have all the elements length equal to zero. We have to output the number of ropes with non-zero lengths after each cut operation.

Let us consider an example for the same −

Let the array length be 10 and elements in the array ( i.e., length of each ropes) be −

ropes[] = { 5, 1, 6, 9, 8, 11, 2, 2, 6, 5 }

For the first iteration, we have rope at index 1 with the smallest length

After cutting 1 unit length from the ropes, and eliminating the zero lengths ropes from the array, the new array obtained is −

ropes[] = { 4, 5, 8, 7, 10, 1, 1, 5, 4 }
and number of ropes left = 9

Now we have smallest ropes at indices 5th and 6th with length = 1 unit.

After cutting unit 1 from all the ropes, and eliminating the zero length ropes, we have the new array obtained as −

ropes[] = { 3, 4, 7, 6, 9, 4, 3 }
and number of ropes left = 7

Similarly, after performing same operation till we have an empty array, the subsequent array lengths are − 9, 7, 5, 3, 2, 1

Let us now discuss the approach to get the desired results −

Approach 1

This is the simple approach in which, we will just iterate over a loop from 0 to n-1, for every iteration, we will find the rope with the smallest length and subtract that length from all the other ropes, now we will count all the ropes left with non-zero lengths and output the number of such ropes. We will keep doing this process until no ropes are left.

This is not the best practice to solve this problem as the time taken for the processing is too long.

Example

Code for this approach is given below −

#include <bits/stdc++.h>
using namespace std;
int main () {
   int rps [] = { 5, 1, 6, 9, 8, 11, 2, 2, 6, 5 } ;
   int nums = 10 ;
   cout << " The ropes left after each cut operation is" << endl;
   while (true) {        
      int min_rope = INT_MAX;
      for (int iterator = 0 ; iterator  < nums  ; iterator++){
         if (rps[iterator] > 0) {
            min_rope = min(min_rope, rps[iterator]);
         }
      }
      if (min_rope == INT_MAX) {
         break;
      }
      int c = 0;
      for (int iterator = 0 ; iterator < nums ; iterator++) {
         if (rps[iterator] > 0) {
            rps[iterator] -= min_rope;
            if (rps[iterator] > 0) {
               c++ ;
            }
         }
      }
      if(c>0){
         cout << c << endl;
      }
   }
   return 0;
}

Output

The ropes left after each cut operation is
9
7
5
3
2
1
  • Time Complexity − The time complexity for this approach is O(N^2) as we are using a loop inside a loop.

  • Space Complexity − Since no extra space is used in this approach, the space complexity is O(1).

Approach 2

In this approach, we will first sort the array and then we will iterate over the sorted array and keep removing the smallest rope each time and counting the number of ropes left after every iteration.

Steps for this approach are given below −

  • Sort the given array containing the length of each rope initially

  • Initially, the length to be cut will be rps[0]

  • Now iterate over the array and if ( rps[i] − length to be cut > 0)

  • If the condition above is true, i.e., if the current rope length in the sorted array is greater than zero, then the length ropes to the right of the present rope will also be greater than zero.

  • So, we just need to print the number of ropes left i.e., (nums - iterator) i.e., number of ropes present to the right of the current rope.

  • Now the next length to be cut will be of length rps[i]

  • Repeat the same process for all the ropes.

Example

The code solution for this approach is written below.

#include <bits/stdc++.h>
using namespace std;
void rpscutting(int rps[], int nums){
   cout<< " The length of ropes left after each cutting operations is " << endl;
   sort(rps, rps + nums);
   int opr = 0;
   int lencut = rps[0];
   for (int iterator = 1; iterator < nums; iterator++){		
      if ( rps[iterator] - lencut > 0){
         cout << (nums - iterator) << " ";
         lencut = rps[iterator];
         opr++;
      }
   }
   if (opr == 0)
      cout << "0 ";
}
int main(){
   int rps[] = { 5, 1, 6, 9, 8, 11, 2, 2, 6, 5 } ;
   int nums = sizeof(rps) / sizeof(rps[0]) ;
   rpscutting(rps, nums) ;
   return 0;
}

Output

The length of ropes left after each cutting operations is 
9 7 5 3 2 1
  • Time complexity − The time complexity of this approach is O(nlogn).

  • Space Complexity − As no extra space is used, the space complexity is O(1).

Conclusion

We discussed two different approaches to solve the problem - Ropes left after every removal of smallest rope. Initial time complexity i.e., time complexity for first approach was O(n^2) which we reduced to O(nlogn) in later approach.

Updated on: 05-Oct-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements