Rope Cutting | Practice | GeeksforGeeks

You are given N ropes. A cut operation is performed 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 operation until the length of each rope is zero.
Example 1:
Input : arr[ ] = {5, 1, 1, 2, 3, 5} 
Output : 4 3 2 
Explanation: In the first operation, the 
minimum ropes are 1 So, we reduce length 1 
from all of them after reducing we left with 
4 ropes and we do the same for rest. 
 
Example 2:
Input : arr[ ] = {5, 1, 6, 9, 8, 11, 2, 
                               2, 6, 5} 
Output :  9 7 5 3 2 1


Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function longest_Subsequence() that takes an array (arr), sizeOfArray (n), and return the number of ropes that are left after each operation with space if no ropes left after one operation, in this case, return 0. The driver code takes care of the printing.
Expected Time Complexity: O(N*LOG(N)).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 105
0
sainithinssjcin 5 hours
class Solution{
    
    // Function for finding maximum and value pair
    public static ArrayList<Integer> RopeCutting (int arr[], int n) {
        // Complete the Function
        Arrays.sort(arr);
        int k = n;
        ArrayList<Integer> ans = new ArrayList<Integer>();
        int count = 1;
        int max= Integer.MIN_VALUE;
        for(int i=0; i<n-1; i++){
            if(arr[i] == arr[i+1]){
                count++;
                if(count>max){
                    max = count;
                }
            }else{
                if(count>max){
                    max = count;
                }
                k = k - max;
                ans.add(k);
                count = 1;
                max= 0;
            }
        }
        return ans;
    }
}
0
abhigyanpatek3 days ago
Simple C++ Solution:
We just need to check the number of remaining elements in every iteration after removing pointer from the minimum 
vector<int> RopeCutting(int arr[], int n)       {           sort(arr, arr+n);           vector<int> v;           int i = 0;           while(i < n){               int minimum = arr[i];               while(arr[i] == minimum) i++;               int r = n-i;               if(r != 0) v.push_back(r);           }           return v;       }
0
arvind16yadav1 month ago
/*JAVA CODE*/
public static ArrayList<Integer> RopeCutting (int arr[], int n) {       // Complete the Function              ArrayList<Integer> ans = new ArrayList<>();              Arrays.sort(arr);              int total = n;       int freq = 1;       int element = arr[0];              for(int i=1; i<n; i++){           if(arr[i] == element){               freq++;           }           else{               total -= freq;               ans.add(total);               freq = 1;               element = arr[i];           }       }              return ans;   }
0
s37randive1 month ago
vector<int> RopeCutting(int a[], int n)
   {
       // Complete the function
       sort(a,a+n);
       vector<int> ans;
       int co,e,l=n;
       for(int i=0;i<n;i++){
           e=a[i];
           co=0;
           while(i<n && a[i]==e){
               co+=1;
               i++;
           }
           i--;
           l-=co;
           if(l==0){
               break;
           }
           ans.push_back(l);
       }
       return ans;
   }
0
rayalravi20013 months ago
c++ solution
 
 vector<int> res;       sort(arr,arr+n);       int cut = arr[0];       for(int i=0; i<n; i++){                      if(arr[i]-cut >0 ){              res.push_back(n-i);           }           cut = arr[i];       }              return res;
0
mohankumarit20013 months ago
    vector<int> RopeCutting(int arr[], int n)
    {
        // Complete the function
        vector<int> res;
        priority_queue<int,vector<int>,greater<int>> q(arr,arr+n);
        while(!q.empty()){
            int cut = q.top();q.pop();
            priority_queue<int,vector<int>,greater<int>> temp;
            while(!q.empty()){
                int val = q.top()-cut;q.pop();
                if(val)temp.push(val);
            }
            if(temp.size())res.push_back(temp.size());
            q = temp;
        }
        return res;
        
        
    }
0
anannyagta4 months ago
 ArrayList<Integer> list = new ArrayList<>();
        Arrays.sort(arr);
        for(int i=0; i<n-1; i++)
        {
            if(arr[i]!=arr[i+1])
            {
                list.add(n-i-1);
            }
        }
        return list;
0
manishkumar04 months ago
public static ArrayList<Integer> RopeCutting (int arr[], int n) {
        ArrayList<Integer>res=new ArrayList<>();
        Arrays.sort(arr);
        int cuttingLen=arr[0];
        for(int i=0;i<n;i++)
        {
            if(arr[i]-cuttingLen>0)
            {
                res.add(n-i);
                cuttingLen+=arr[i]-cuttingLen;
            }
            
        }
        return res;
    }
0
chessnoobdj4 months ago
C++
vector<int> RopeCutting(int arr[], int n)
    {
        vector <int> res;
        sort(arr, arr+n);
        int cnt = 0, i=0;
        while(i<n){
            while(i<n-1 && arr[i] == arr[i+1]){
                i += 1;
                cnt += 1;
            }
            cnt += 1;
            res.push_back(n-cnt);
            i += 1;
        }
        res.pop_back();
        return res;
    }
-5
neznajko7 months ago
0.7/5.9 Python
class Solution:
    def RopeCutting( self, arr, n):
        arr = sorted( arr)
        res = []
        # add guardian
        arr.append( 0) # no negative length ropes
        i, j = 0, 1
        while True:
            while arr[ j] == arr[ i]: j += 1
            if j == n: break
            res.append( n - j)
            i = j
            j += 1
        return res
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.