Rod Cutting | Practice | GeeksforGeeks

Given a rod of length N inches and an array of prices, price[] that contains prices of all pieces of size smaller than N. Determine the maximum value obtainable by cutting up the rod and selling the pieces.
 
Example 1:
Input:
N = 8
Price[] = {1, 5, 8, 9, 10, 17, 17, 20}
Output:
22
Explanation:
The maximum obtainable value is 22 by
cutting in two pieces of lengths 2 and 
6, i.e., 5+17=22.

Example 2:
Input:
N=8
Price[] = {3, 5, 8, 9, 10, 17, 17, 20}
Output: 24
Explanation: 
The maximum obtainable value is 
24 by cutting the rod into 8 pieces 
of length 1, i.e, 8*3=24. 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function cutRod() which takes the array A[] and its size N as inputs and returns the maximum price obtainable.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 1000
1 ≤ Ai ≤ 105
+1
adarshgupta401in 5 hours
Easy Java DP Solution-
class Solution{
    public int cutRod(int price[], int n) {
        //Same as unbounded KnapSack just - unlimited amount of an cut part can be taken (multiple ocuurences allowed)
        /*
        val[] = price[]
        wt[] = length[] = {1,2,3,  , N-1, N};
        W = capacity = N = length of rod
        */
        int[][] dp = new int[n+1][n+1]; // rows for length of price arr (allowed  piece's prices) , colums for sum prices of rods pieces
        for(int i=0; i<n+1 ; i++) dp[0][i] = 0; 
        for(int i= 0; i<n+1 ; i++) dp[i][0] = 0;
        
        int [] length = new int[n];
        for(int i=0 ; i< n ; i++) length[i] = i+1;   //length[] = {1,2,3  ,n};
        
        for(int i=1 ; i< n+1 ; i++){
            for(int j=1 ; j< n+1 ; j++){
                if(length[i-1] <= j )
                    dp[i][j] = Math.max( dp[i-1][j] , //Excliding this size 
                                price[i-1] + dp[i][j- length[i-1]] ); // including this size/length 
                else dp[i][j] = dp[i-1][j];
            }
        }
        
        return dp[n][n];
    }
}
0
gfgraj3 days ago
class Solution{ public:   int cutRod(int price[], int n) {       int arr[n];       arr[0]=0;       for(int i=1;i<=n;i++){           int ans=INT_MIN;           for(int j=0;j<i;j++){               ans=max(ans,price[j]+arr[i-j-1]);           }           arr[i]=ans;       }       return arr[n];   }};
+1
sarthakbhatnagar9991 week ago
CLASSIC UNBOUNDED KNAPSACK PROBLEM ->
int cutRod(int price[], int n) {
        //code here
        int N =n;
        int t[N+1][n+1];
        for(int i=0;i<N+1;i++)
        {
            for(int j=0;j<n+1;j++)
            {
                if(i==0|| j==0)
                {
                    t[i][j] = 0;
                }
            }
        }
        int len[N];
        for(int i=0;i<N;i++)
        {
            len[i] = i+1;
        }
        for(int i=1;i<N+1;i++)
        {
            for(int j=1;j<n+1;j++)
            {
                if(len[i-1]<=j)
                {
                    t[i][j] = max(price[i-1]+t[i][j-len[i-1]],t[i-1][j]);
                }
                else
                {
                    t[i][j] = t[i-1][j];
                }
            }
        }
        return t[N][n];
    }
0
pawan kumar 331 week ago
Working C++ Code:- 
 
int cutRod(int price[], int n) {           int res[n];           res[0] = price[0];           for(int i=1;i<n;i++)           {               int st = 0,end = i-1;               int maxx = price[i];               while(st<=end)               {                   int t = res[st]+res[end];                   if(t>maxx)                       maxx = t;                   st++;                   end--;               }               res[i] = maxx;           }           return res[n-1];   }
0
prasadchaskar1 week ago
Python Solution:
class Solution:
    def cutRod(self, price, n):
        arr = [i for i in range(1,n+1)]
        t = [[0 for i in range(n+1)] for j in range(n+1)]
        for  i in range(n+1):
            for j in range(n+1):
                if i==0 or j==0:
                    t[i][j] = 0
                elif arr[i-1] <= j:
                    t[i][j] = max( (price[i-1]+t[i][j-arr[i-1]]), (t[i-1][j]) )
                else:
                    t[i][j] = t[i-1][j]
        return t[n][n]
0
f201806811 week ago
class Solution{ public:   int cutRod(int price[], int n) {       //code here              int arr[n] = {0};       for(int i =0; i<=n; i++){           arr[i] = i+1;       }              int dp[n+1][n+1];              for(int i =0; i<= n; i++){           for(int j =0; j<=n ; j++){               if(i == 0 or j == 0){                   dp[i][j] = 0;               }           }       }              for(int i = 1; i <= n; i ++){           for(int j = 1; j <= n ; j ++){                              if(arr[i-1] <= j){                   dp[i][j] = max(dp[i-1][j] , dp[i][j-arr[i-1]] + price[i-1]);               }                              else{                   dp[i][j] = dp[i-1][j];               }           }       }              return dp[n][n];   }};
0
aswalroshan20021 week ago
class Solution{
  public:
    int cutRod(int price[], int n) {
        int arr[n],sum=0;
        for(int i=1;i<=n;i++)arr[i-1]=i;
        int dp[n+1][n+1];
        for(int i=0;i<=n;i++){
            dp[i][0]=dp[0][i]=0;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(arr[i-1]<=j)
                    dp[i][j]=max(dp[i-1][j],price[i-1]+dp[i][j-arr[i-1]]);
                else
                    dp[i][j]=dp[i-1][j];
            }
        }
        return dp[n][n];
    }
};
+1
amaaniqbal7862 weeks ago
0.01 SECONDS // C++ DP APPROACH
class Solution{ public:   int dp[1001];   int solve(int price[], int n){       if(n == 0){return 0;}       if(n == 1){return dp[1] = price[0];}       if(dp[n] != -1){return dp[n];}              int maxi = INT_MIN;       for(int i=1; i<=n; i++){           maxi = max(maxi, price[i-1] + solve(price,n-i));       }       return dp[n] = maxi;   }   int cutRod(int price[], int n) {       dp[n+1];       for(int i=0;i<=n;i++){dp[i] = -1;}       dp[0] = 0;       return solve(price,n);   }};
0
aniketsaraswat1123 weeks ago
Bottom-Up DP approach:
int cutRod(int arr[], int n) {
        int dp[n+1] = {};
        dp[1] = arr[0];
        for(int i = 2; i<n+1; i++){
            int ans = -1;
            for(int j = 1; j<i; j++)
                ans = max(ans,dp[j] + arr[i-j-1]);
            dp[i] = max(arr[i-1],ans);
        }
        return dp[n];
    }
0
aniketsaraswat112
This comment was deleted.
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.