Open In App

Rod Cutting

Last Updated : 12 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Medium
60.66%
1.4L

Given a rod of length n inches and an array of prices that includes prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. We are mainly given a price array such that the size of the price array is same as rod length and price[i] represents price of length i+1

Input : price[] = { 1 , 5, 8, 9, 10, 17 , 17 . 20}
Output : 22
Explanation : Rod length is 8 and the values of different pieces are given as the following,
length | 1 2 3 4 5 6 7 8
——————————
price | 1 5 8 9 10 17 17 20
The maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6) 

Input : price[] = { 3 , 5, 8, 9, 10, 17}
Output : 18
Explanation : Rod length is 7 And if the prices are as follows.
length | 1 2 3 4 5 6
————————
price | 3 4 6 7 9 10
The maximum obtainable value is 18 (by making 6 pieces each of length 1)

Input : price[] = { 3 }
Output : 3
Explanation: There is only 1 way to pick a piece of length 1

Naive Recursive Solution:

We generate all configurations of different pieces and find the highest-priced configuration. This solution is exponential in terms of time complexity. Let us see how this problem possesses both important properties of a Dynamic Programming (DP) Problem and can efficiently be solved using Dynamic Programming.

1) Optimal Substructure: 

We can get the best price by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut.

Let cutRod(n) be the required (best possible price) value for a rod of length n. It can be written as follows.
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}

C++
// A recursive solution for Rod cutting problem
#include <bits/stdc++.h>
using namespace std;

/* Returns the best obtainable price for a rod of length n
   and price[] as prices of different pieces */
int cutRod(int price[], int n)
{
    // if n is 0 we cannot cut the rod anymore.
    if (n ==0) {
       return 0;}
  
    int res = 0;
    for (int i=0; i<n; i++) 
       res = max(res, price[i] + cutRod(price, n-i-1));
  
    return res;
}

/* Driver program to test above functions */
int main()
{
    int arr[] = { 0, 5, 8, 9, 10, 17, 17, 20 };
    int size = sizeof(arr) / sizeof(arr[0]);
    cout << cutRod(arr, size);
    return 0;
}
C
#include <stdio.h>
#include <limits.h>

int cutRod(int price[], int idx, int n) {
  
    // Base case: if index is 0, then the only option
    // is to take multiple pieces of length 1
    if (idx == 0) return n * price[0];
  
    // Base case: if the rod length is 0, no
    // more cuts can be made
    if (n == 0) return 0;

    // Option 1: Do not cut the rod at this index
    int notCut = cutRod(price, idx - 1, n);
    int cut = INT_MIN;
    int rodLen = idx + 1;

    // Option 2: Cut the rod if the current 
    // length is within bounds
    if (rodLen <= n)
        cut = price[idx] + cutRod(price, idx, n - rodLen);

    // Return the maximum value between
    // cutting and not cutting
    return (notCut > cut) ? notCut : cut;
}

int main() {
    int price[] = {1, 5, 8, 9, 10, 17, 17, 20};
    int size = sizeof(price) / sizeof(price[0]);
    printf("%d\n", cutRod(price, size - 1, size));
    return 0;
}
Java
// Java recursive solution for Rod cutting problem
import java.io.*;
class GFG {

    /* Returns the best obtainable price for a rod of length
    n and price[] as prices of different pieces */
    static int cutRod(int price[], int index, int n)
    {
        // base case
      // if index ==0 that means we can get n number of pieces of unit length with price price[0]
        if (index == 0) {
            return n * price[0];
        }
      // if n, the size of the rod is 0 we cannot cut the rod anymore.
      if (n ==0) {
        return 0;
      }
      
        // At any index we have 2 options either
        // cut the rod of this length or not cut
        // it
        int notCut = cutRod(price, index - 1, n);
        int cut = Integer.MIN_VALUE;
        int rod_length = index + 1;
        int new_length = n - rod_length;
      
      // rod can be cut only if current rod length is than actual length of the rod
      
        if (rod_length <= n)
        //next cutting index can only be new length - 1
            cut = price[index]
                  + cutRod(price, new_length-1, new_length);

        return Math.max(notCut, cut);
    }

    /* Driver program to test above functions */
    public static void main(String args[])
    {
        int arr[] = { 1, 5, 8, 9, 10, 17, 17, 20};
        int size = arr.length;
        System.out.println("Maximum Obtainable Value is "
                           + cutRod(arr, size - 1, size));
    }
}

// This code is contributed by saikrishna padamatinti
Python
#  A recursive solution for Rod cutting problem

#  Returns the best obtainable price for a rod of length n
#   and price[] as prices of different pieces 
def cutRod(price, index, n):
    
    #  base case
    if index == 0:
        return n*price[0]
      
      #v if n is 0 we cannot cut the rod anymore.
    if (n ==0):
        return 0
    
    #   At any index we have 2 options either
    #   cut the rod of this length or not cut 
    #   it
    notCut = cutRod(price,index - 1,n)
    cut = float("-inf")
    rod_length = index + 1

    if (rod_length <= n):
        
        cut = price[index]+cutRod(price,index,n - rod_length)
  
    return max(notCut, cut)

#  Driver program to test above functions 
arr = [ 1, 5, 8, 9, 10, 17, 17, 20 ]
size = len(arr)
print("Maximum Obtainable Value is ",cutRod(arr, size - 1, size))

# This code is contributed by Vivek Maddeshiya
C#
// C# recursive solution for Rod cutting problem

using System;

public class GFG {
  static int max(int a, int b) {
    if (a > b) return a;
    return b;
  }

  static int MIN_VALUE = -1000000000;

  /* Returns the best obtainable price for a rod of 
  // length n and price[] as prices of different pieces */
  static int cutRod(int[] price, int index, int n) {
    // base case
    if (index == 0) {
      return n * price[0];
    }
    // if n is 0 we cannot cut the rod anymore.
      if (n ==0) {
        return 0;
      }
    
    // At any index we have 2 options either 
    // cut the rod of this length or not cut it
    int notCut = cutRod(price, index - 1, n);
    int cut = MIN_VALUE;
    int rod_length = index + 1;

    if (rod_length <= n)
      cut = price[index] + cutRod(price, index, n - rod_length);

    return max(notCut, cut);
  }

  // Driver program to test above functions
  public static void Main(string[] args) {
    int[] arr = {1, 5, 8, 9, 10, 17, 17, 20 };
    int size = arr.Length;
    Console.WriteLine("Maximum Obtainable Value is " + 
                      cutRod(arr, size - 1, size));
  }
}

// This code is contributed by ajaymakavana.
JavaScript
// A recursive solution for Rod cutting problem

/* Returns the best obtainable price for a rod of length n
   and price[] as prices of different pieces */
function cutRod(price,  index,  n)
{
    // base case
    if (index == 0) {
        return n * price[0];
    }
    // if n is 0 we cannot cut the rod anymore.
      if (n ==0) {
        return 0;
        }
    //At any index we have 2 options either
      //cut the rod of this length or not cut 
      //it
    let notCut = cutRod(price,index - 1,n);
    let cut = Number.MIN_VALUE;
    let rod_length = index + 1;

    if (rod_length <= n)
        cut = price[index]
               + cutRod(price,index,n - rod_length);
  
    return Math.max(notCut, cut);
}

/* Driver program to test above functions */

    let arr = [ 1, 5, 8, 9, 10, 17, 17, 20 ];
    let size = arr.length;
    console.log("Maximum Obtainable Value is "
         + cutRod(arr, size - 1, size));
   
   // This code is contributed by garg28harsh.

Output
22

Time Complexity: Exponential
Space Complexity: O(n) where n is the length of the price array.


2) Overlapping Subproblems

Considering the above implementation, the following is the recursion tree for a Rod of length 4. 

cR() ---> cutRod() 
cR(4)
/ /
/ /
cR(3) cR(2) cR(1) cR(0)
/ | / |
/ | / |
cR(2) cR(1) cR(0) cR(1) cR(0) cR(0)
/ | |
/ | |
cR(1) cR(0) cR(0) cR(0)
/
/
CR(0)

In the above partial recursion tree, cR(2) is solved twice. We can see that there are many subproblems that are solved again and again. Since the same subproblems are called again, this problem has the Overlapping Subproblems property. So the Rod Cutting problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of the same subproblems can be avoided by either using memoization or tabulation.

Memoization (or Top-Down) Solution:

  1. We use 1D memo array. The range of values change from 0 to n, we make an array of size n+1.
  2. We initialize the memo array as -1 to indicate that all values are not computed initially.
  3. Before we make a recursive call, we check if the value is already computed by checking for -1 in memo. After we compute values, we store them in memo.
C++
#include <bits/stdc++.h>
using namespace std;

/* The memo array is used to check if a solution
   is already computed or not */
int cutRodUtil(int price[], int n, vector<int>& memo) {
  
    // if n is 0, we cannot cut the rod anymore.
    if (n == 0) return 0;

    // If the result is already computed, return it.
    if (memo[n] != -1) return memo[n];

    int res = 0;
    for (int i = 0; i < n; i++) 
        res = max(res, price[i] + 
                cutRodUtil(price, n - i - 1, memo));

    // Store the computed result in memoization 
    // table and return it.
    return memo[n] = res;
}

int cutRod(int price[], int n) {
  
    // Initialize memoization table with
    // -1 to indicate uncomputed states.
    vector<int> memo(n + 1, -1);
  
    return cutRodUtil(price, n, memo);
}

/* Driver program to test above functions */
int main() {
    int arr[] = { 0, 5, 8, 9, 10, 17, 17, 20 };
    int size = sizeof(arr) / sizeof(arr[0]);
    cout << cutRod(arr, size);
    return 0;
}
Java
// A memoization solution for Rod cutting problem
import java.io.*;
import java.util.*;

/* Returns the best obtainable price for a rod of length n
and price[] as prices of different pieces */
class GFG {
  private static int cutRod(int price[], int index, int n,
                            int[][] dp)
  {
    
    // base case
    if (index == 0) {
      return n * price[0];
    }

    if (dp[index][n] != -1) {
      return dp[index][n];
    }

    // At any index we have 2 options either
    // cut the rod of this length or not cut
    // it
    int notCut = cutRod(price, index - 1, n, dp);
    int cut = Integer.MIN_VALUE;
    int rod_length = index + 1;

    if (rod_length <= n) {
      cut = price[index]
        + cutRod(price, index, n - rod_length,
                 dp);
    }

    return dp[index][n] = Math.max(cut, notCut);
  }

  /* Driver program to test above functions */
  public static void main(String[] args)
  {
    int arr[] = { 1, 5, 8, 9, 10, 17, 17, 20 };
    int size = arr.length;
    int dp[][] = new int[size][size + 1];
    for (int i = 0; i < size; i++) {
      Arrays.fill(dp[i], -1);
    }
    System.out.println(
      "Maximum Obtainable Value is "
      + cutRod(arr, size - 1, size, dp));
  }
}

// This code is contributed by Snigdha Patil
Python
# A memoization solution for Rod cutting problem

 # Returns the best obtainable price for 
  # a rod of length n and price[] as 
  # prices of different pieces
def cutRoad(price,index,n,dp):
  
    # base case
    if(index == 0):
        return n*price[0]
    if(dp[index][n] != -1):
        return dp[index][n]
      
    # At any index we have 2 options either 
    # cut the rod of this length or not cut it
    notCut = cutRoad(price,index-1,n,dp)
    cut = -5
    rod_length = index + 1
    if(rod_length <= n):
        cut = price[index] + cutRoad(price,index,n-rod_length,dp)
    dp[index][n] = max(notCut,cut)
    return dp[index][n]

# Driver program to test above functions
if __name__ == "__main__":
    arr = [1,5,8,9,10,17,17,20]
    size = len(arr)
    dp = []
    temp = []
    for i in range(0,size+1):
        temp.append(-1)
    for i in range(0,size):
        dp.append(temp)
    # print(dp)
    print("Maximum Obtainable Value is :",end=' ')
    print(cutRoad(arr,size-1,size,dp))
C#
// A memoization solution for Rod cutting problem
using System;

public class HelloWorld 
{
  
  // Returns the best obtainable price for 
  // a rod of length n and price[] as prices of different pieces
  private static int cutRod(int[] price, int index, 
                            int n, int[,] dp) 
  {

    // base case
    if (index == 0) {
      return n * price[0];
    }

    if (dp[index, n] != -1) {
      return dp[index, n];
    }

    // At any index we have 2 options either cut 
    // the rod of this length or not cut it
    int notCut = cutRod(price, index - 1, n, dp);
    int cut = Int32.MinValue;
    int rod_length = index + 1;

    if (rod_length <= n) {
      cut = price[index] + cutRod(price, index, n - rod_length, dp);
    }

    return dp[index, n] = Math.Max(cut, notCut);
  }

  //  Driver program to test above functions
  public static void Main(string[] args) {
    int[] arr = { 1, 5, 8, 9, 10, 17, 17, 20 };
    int size = arr.Length;
    int[,] dp = new int[size, size + 1];
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size + 1; j++) {
        dp[i, j] = -1;
      }
    }
    Console.WriteLine("Maximum Obtainable Value is " + 
                      cutRod(arr, size - 1, size, dp));
  }
}

// This code is contributed by ajaymakvana.
JavaScript
// A memoization solution for Rod cutting problem

// A utility function to get the maximum of two integers
function max(a, b) {
    return (a > b) ? a : b;
}

/* Returns the best obtainable price for a rod of length n
and price[] as prices of different pieces */
function cutRod(price, index, n, dp) {
  // base case
  if (index === 0) {
      return n * price[0];
  }
  if (dp[index][n] !== -1)
      return dp[index][n];

  // At any index we have 2 options either
  // cut the rod of this length or not cut
  // it
  let notCut = cutRod(price, index - 1, n, dp);
  let cut = Number.MIN_SAFE_INTEGER;
  let rod_length = index + 1;

  if (rod_length <= n)
      cut = price[index] + cutRod(price, index, n - rod_length, dp);

  return dp[index][n] = max(notCut, cut);
}

/* Driver program to test above functions */
function main() {
  let arr = [ 1, 5, 8, 9, 10, 17, 17, 20 ];
  let size = arr.length;
  let dp = new Array(size);
  for (let i = 0; i < size; i++) {
      dp[i] = new Array(size + 1).fill(-1);
  }
  console.log("Maximum Obtainable Value is " + cutRod(arr, size - 1, size, dp));
  return 0;
}

main();

Output
22

Time Complexity: O(n)
Auxiliary Space: O(n) 

Dynamic Programming (Bottom Up) Solution:

  • We make a 1D array as there is one parameter that changes in recursion and size of this array is n+1 as the range goes from 0 to n.
  • We use a loop inside the main loop to fill the dp array in bottom up manner.
C++
#include <bits/stdc++.h>
using namespace std;

/* Returns the best obtainable price for a 
   rod of length n and price as prices of 
   different pieces */
int cutRod(vector<int>& price) {
  
    int n = price.size();
  
    // Create a dp array to store the maximum 
    // revenue obtainable for each length
    vector<int> dp(n + 1, 0);

    // Build the dp array in a bottom-up manner
    for (int i = 1; i <= n; i++) {
        int max_val = 0;
        for (int j = 0; j < i; j++) {
            max_val = max(max_val, price[j] + dp[i - j - 1]);
        }
        dp[i] = max_val;
    }
  
    return dp[n];
}

/* Driver program to test above functions */
int main() {
    vector<int> price = { 0, 5, 8, 9, 10, 17, 17, 20 };
    cout << cutRod(price);
    return 0;
}
C
// A Dynamic Programming solution for Rod cutting problem
#include<stdio.h>
#include<limits.h>

// A utility function to get the maximum of two integers
int max(int a, int b) { return (a > b)? a : b;}

/* Returns the best obtainable price for a rod of length n and
   price[] as prices of different pieces */
int cutRod(int price[], int n)
{
   int val[n+1];
   val[0] = 0;
   int i, j;

   // Build the table val[] in bottom up manner and return the last entry
   // from the table
   for (i = 1; i<=n; i++)
   {
       int max_val = INT_MIN;
       for (j = 0; j < i; j++)
         max_val = max(max_val, price[j] + val[i-j-1]);
       val[i] = max_val;
   }

   return val[n];
}

/* Driver program to test above functions */
int main()
{
    int arr[] = {1, 5, 8, 9, 10, 17, 17, 20};
    int size = sizeof(arr)/sizeof(arr[0]);
    printf("Maximum Obtainable Value is %d", cutRod(arr, size));
    getchar();
    return 0;
}
Java
// A Dynamic Programming solution for Rod cutting problem
import java.io.*;
class RodCutting
{
    /* Returns the best obtainable price for a rod of
       length n and price[] as prices of different pieces */
    static int cutRod(int price[],int n)
    {
        int val[] = new int[n+1];
        val[0] = 0;

        // Build the table val[] in bottom up manner and return
        // the last entry from the table
        for (int i = 1; i<=n; i++)
        {
            int max_val = Integer.MIN_VALUE;
            for (int j = 0; j < i; j++)
                max_val = Math.max(max_val, 
                                   price[j] + val[i-j-1]);
            val[i] = max_val;
        }

        return val[n];
    }

    /* Driver program to test above functions */
    public static void main(String args[])
    {
        int arr[] = new int[] {1, 5, 8, 9, 10, 17, 17, 20};
        int size = arr.length;
        System.out.println("Maximum Obtainable Value is " +
                            cutRod(arr, size));
    }
}
/* This code is contributed by Rajat Mishra */
Python
# A Dynamic Programming solution for Rod cutting problem
INT_MIN = -32767

# Returns the best obtainable price for a rod of length n and
# price[] as prices of different pieces
def cutRod(price, n):
    val = [0 for x in range(n+1)]
    val[0] = 0

    # Build the table val[] in bottom up manner and return
    # the last entry from the table
    for i in range(1, n+1):
        max_val = INT_MIN
        for j in range(i):
             max_val = max(max_val, price[j] + val[i-j-1])
        val[i] = max_val

    return val[n]

# Driver program to test above functions
arr = [1, 5, 8, 9, 10, 17, 17, 20]
size = len(arr)
print("Maximum Obtainable Value is " + str(cutRod(arr, size)))

# This code is contributed by Bhavya Jain
C#
// A Dynamic Programming solution 
// for Rod cutting problem
using System;
class GFG {

    /* Returns the best obtainable 
       price for a rod of length n
       and price[] as prices of 
       different pieces */
    static int cutRod(int []price,int n)
    {
        int []val = new int[n + 1];
        val[0] = 0;

        // Build the table val[] in
        // bottom up manner and return
        // the last entry from the table
        for (int i = 1; i<=n; i++)
        {
            int max_val = int.MinValue;
            for (int j = 0; j < i; j++)
                max_val = Math.Max(max_val, 
                          price[j] + val[i - j - 1]);
            val[i] = max_val;
        }

        return val[n];
    }
    
    // Driver Code
    public static void Main()
    {
        int []arr = new int[] {1, 5, 8, 9, 10, 17, 17, 20};
        int size = arr.Length;
        Console.WriteLine("Maximum Obtainable Value is " +
                                        cutRod(arr, size));
        
    }
}

// This code is contributed by Sam007
JavaScript
    // A Dynamic Programming solution
    // for Rod cutting problem
    
    /* Returns the best obtainable
       price for a rod of length n
       and price[] as prices of
       different pieces */
    function cutRod(price, n)
    {
        let val = new Array(n + 1);
        val[0] = 0;
 
        // Build the table val[] in
        // bottom up manner and return
        // the last entry from the table
        for (let i = 1; i<=n; i++)
        {
            let max_val = Number.MIN_VALUE;
            for (let j = 0; j < i; j++)
                max_val = Math.max(max_val, price[j] + val[i - j - 1]);
            val[i] = max_val;
        }
 
        return val[n];
    }
    
    let arr = [1, 5, 8, 9, 10, 17, 17, 20];
    let size = arr.length;
    console.log("Maximum Obtainable Value is " + cutRod(arr, size) + "n");
PHP
<?php
// A Dynamic Programming solution for
// Rod cutting problem

/* Returns the best obtainable price 
for a rod of length n and price[] as
prices of different pieces */
function cutRod( $price, $n)
{
    $val = array();
    $val[0] = 0;
    $i; $j;
    
    // Build the table val[] in bottom 
    // up manner and return the last
    // entry from the table
    for ($i = 1; $i <= $n; $i++)
    {
        $max_val = PHP_INT_MIN;
        
        for ($j = 0; $j < $i; $j++)
            $max_val = max($max_val, 
             $price[$j] + $val[$i-$j-1]);
        $val[$i] = $max_val;
    }
    
    return $val[$n];
}

// Driver program to test above functions
$arr = array(1, 5, 8, 9, 10, 17, 17, 20);
$size = count($arr);
echo "Maximum Obtainable Value is ", 
                      cutRod($arr, $size);
    
// This code is contributed by anuj_67.
?>

Output
22

Time Complexity : O(n^2)
Space Complexity: O(n)

Note : The above provided solution works better than the below solution as the below solution requires more extra space and computations.

Using the idea of Unbounded Knapsack:

This problem is very similar to the Unbounded Knapsack Problem, where there are multiple occurrences of the same item. Here we consider length of rod as capacity of knapsack. All lengths from 1 to n are considers are weights of items and prices as values of items.

Recursive Solution:

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Simple implementation of unbounded 
// knapsack problem.
int rodCutRec(int n, vector<int>& len,
              vector<int>& price, int idx) {
  
    // Base Case: if the rod length is 0 
    // or no pieces left to consider
    if (idx == 0) {
        return (n / len[0]) * price[0];
    }

    // If not taking the current length piece
    int notTake = rodCutRec(n, len, price, 
                                idx - 1);

    // If taking the current length piece
    int take = 0;
    if (len[idx] <= n) {
        take = price[idx] + rodCutRec(n-len[idx], 
                                len, price, idx);
    }

    // Return the maximum value between taking 
    // and not taking the current piece
    return max(take, notTake);
}

int rodCut(vector<int>& price) {
    int n = price.size();
  
    // Set up weights for unbounded
    // knapsack problem
    vector<int> len(n);
    for (int i = 0; i < n; ++i)
        len[i] = i + 1;  
 
    return rodCutRec(n, len, price, n - 1);
}

// Driver code
int main() {
    vector<int> price = {10, 30, 20};
    cout << rodCut(price) ;
    return 0;
}
C
// C program for above approach
#include <stdio.h>
#include <stdlib.h>

int max(int a, int b) 
{ 
  return (a > b) ? a : b; 
}

// Global Array for the
// purpose of memoization.
int t[9][9];

// A recursive program, using ,
// memoization, to implement the
// rod cutting problem(Top-Down).
int un_kp(int price[], int length[], 
                     int Max_len, int n)
{

    // The maximum price will be zero,
    // when either the length of the rod
    // is zero or price is zero.
    if (n == 0 || Max_len == 0) 
    {
        return 0;
    }

    // If the length of the rod is less
    // than the maximum length, Max_lene
    // will consider it.Now depending
    // upon the profit,
    // either Max_lene  we will take it
    // or discard it.
    if (length[n - 1] <= Max_len) 
    {
        t[n][Max_len]
            = max(price[n - 1]
                      + un_kp(price, length,
                              Max_len - length[n - 1], n),
                  un_kp(price, length, Max_len, n - 1));
    }

    // If the length of the rod is greater
    // than the permitted size, Max_len
    // we will  not consider it.
    else 
    {
        t[n][Max_len]
            = un_kp(price, length,
                             Max_len, n - 1);
    }

    // Max_lene Max_lenill return
    // the maximum value obtained,
    // Max_lenhich is present at the
    // nth roMax_len and Max_length column.
    return t[n][Max_len];
}

/* Driver program to test above functions */
int main()
{
    int price[] = { 1, 5, 8, 9, 10, 17, 17, 20 };
    int n = sizeof(price) / sizeof(price[0]);
    int length[n];
    for (int i = 0; i < n; i++) 
    {
        length[i] = i + 1;
    }
    int Max_len = n;

    // Function Call
    printf("Maximum obtained value  is %d \n",
           un_kp(price, length, n, Max_len));
}
Java
// Java program for above approach
import java.io.*;

class GFG {

    // Global Array for
    // the purpose of memoization.
    static int t[][] = new int[9][9];

    // A recursive program, using ,
    // memoization, to implement the
    // rod cutting problem(Top-Down).
    public static int un_kp(int price[], int length[],
                            int Max_len, int n)
    {

        // The maximum price will be zero,
        // when either the length of the rod
        // is zero or price is zero.
        if (n == 0 || Max_len == 0) {
            return 0;
        }

        // If the length of the rod is less
        // than the maximum length, Max_lene will
        // consider it.Now depending
        // upon the profit,
        // either Max_lene  we will take
        // it or discard it.
        if (length[n - 1] <= Max_len) {
            t[n][Max_len] = Math.max(
                price[n - 1]
                    + un_kp(price, length,
                            Max_len - length[n - 1], n),
                un_kp(price, length, Max_len, n - 1));
        }

        // If the length of the rod is
        // greater than the permitted size,
        // Max_len we will  not consider it.
        else {
            t[n][Max_len]
                = un_kp(price, length, Max_len, n - 1);
        }

        // Max_lene Max_lenill return the maximum
        // value obtained, Max_lenhich is present
        // at the nth roMax_len and Max_length column.
        return t[n][Max_len];
    }

    public static void main(String[] args)
    {

        int price[]
            = new int[] { 1, 5, 8, 9, 10, 17, 17, 20 };
        int n = price.length;
        int length[] = new int[n];
        for (int i = 0; i < n; i++) {
            length[i] = i + 1;
        }
        int Max_len = n;
        System.out.println(
            "Maximum obtained value is "
            + un_kp(price, length, n, Max_len));
    }
}

// This code is contributed by rajsanghavi9.
Python
# Python program for above approach

# Global Array for
# the purpose of memoization.
t = [[0 for i in range(9)] for j in range(9)]

# A recursive program, using ,
# memoization, to implement the
# rod cutting problem(Top-Down).
def un_kp(price, length, Max_len, n):

    # The maximum price will be zero,
    # when either the length of the rod
    # is zero or price is zero.
    if (n == 0 or Max_len == 0):
        return 0;
    

    # If the length of the rod is less
    # than the maximum length, Max_lene will
    # consider it.Now depending
    # upon the profit,
    # either Max_lene we will take
    # it or discard it.
    if (length[n - 1] <= Max_len):
        t[n][Max_len] = max(price[n - 1] + un_kp(price, length, Max_len - length[n - 1], n),
                un_kp(price, length, Max_len, n - 1));
    

    # If the length of the rod is
    # greater than the permitted size,
    # Max_len we will not consider it.
    else:
        t[n][Max_len] = un_kp(price, length, Max_len, n - 1);
    

    # Max_lene Max_lenill return the maximum
    # value obtained, Max_lenhich is present
    # at the nth roMax_len and Max_length column.
    return t[n][Max_len];


if __name__ == '__main__':

    price = [1, 5, 8, 9, 10, 17, 17, 20 ];
    n =len(price);
    length = [0]*n;
    for i in range(n):
        length[i] = i + 1;
    
    Max_len = n;
    print("Maximum obtained value is " ,un_kp(price, length, n, Max_len));

# This code is contributed by gauravrajput1 
C#
// C# program for above approach
using System;
public class GFG {

  // Global Array for
  // the purpose of memoization.
  static int [,]t = new int[9,9];

  // A recursive program, using ,
  // memoization, to implement the
  // rod cutting problem(Top-Down).
  public static int un_kp(int []price, int []length, int Max_len, int n) {

    // The maximum price will be zero,
    // when either the length of the rod
    // is zero or price is zero.
    if (n == 0 || Max_len == 0) {
      return 0;
    }

    // If the length of the rod is less
    // than the maximum length, Max_lene will
    // consider it.Now depending
    // upon the profit,
    // either Max_lene we will take
    // it or discard it.
    if (length[n - 1] <= Max_len) {
      t[n,Max_len] = Math.Max(price[n - 1] + un_kp(price, length, Max_len - length[n - 1], n),
                              un_kp(price, length, Max_len, n - 1));
    }

    // If the length of the rod is
    // greater than the permitted size,
    // Max_len we will not consider it.
    else {
      t[n,Max_len] = un_kp(price, length, Max_len, n - 1);
    }

    // Max_lene Max_lenill return the maximum
    // value obtained, Max_lenhich is present
    // at the nth roMax_len and Max_length column.
    return t[n,Max_len];
  }

  // Driver code
  public static void Main(String[] args) {

    int []price = new int[] { 1, 5, 8, 9, 10, 17, 17, 20 };
    int n = price.Length;
    int []length = new int[n];
    for (int i = 0; i < n; i++) {
      length[i] = i + 1;
    }
    int Max_len = n;
    Console.WriteLine("Maximum obtained value is " + un_kp(price, length, n, Max_len));
  }
}

// This code is contributed by gauravrajput1.
JavaScript
// Javascript program for above approach

// Global Array for
// the purpose of memoization.
let t = new Array(9);
for (var i = 0; i < t.length; i++) {
    t[i] = new Array(2);
}

// A recursive program, using ,
// memoization, to implement the
// rod cutting problem(Top-Down).
function un_kp(price, length, Max_len, n)
{

    // The maximum price will be zero,
    // when either the length of the rod
    // is zero or price is zero.
    if (n == 0 || Max_len == 0) 
    {
        return 0;
    }

    // If the length of the rod is less
    // than the maximum length, Max_lene will
    // consider it.Now depending 
    // upon the profit,
    // either Max_lene  we will take 
    // it or discard it.
    if (length[n - 1] <= Max_len) 
    {
        t[n][Max_len]
            = Math.max(price[n - 1]
                      + un_kp(price, length,
                           Max_len - length[n - 1], n),
                  un_kp(price, length, Max_len, n - 1));
    }

    // If the length of the rod is
    // greater than the permitted size,
    // Max_len we will  not consider it.
    else 
    {
        t[n][Max_len]
            = un_kp(price, length, 
                              Max_len, n - 1);
    }

    // Max_lene Max_lenill return the maximum
    // value obtained, Max_lenhich is present
    // at the nth roMax_len and Max_length column.
    return t[n][Max_len];
}
    
    // Driver code
    
    let price = [ 1, 5, 8, 9, 10, 17, 17, 20 ];
    let n = price.length;
    let length = Array(n).fill(0);
    for (let i = 0; i < n; i++) {
        length[i] = i + 1;
    }
    let Max_len = n;

    // Function Call
    console.log("Maximum obtained value  is "
         + un_kp(price, length, n, Max_len));

Output
40

The above solution can be optimized either using memoization or dynamic programming.

Dynamic Programming Solution

  • We use a 2D array as there are two parameters that change during recursive calls.
  • We use 1 dimension for index and one for size of rod.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Function to return the maximum value that
// can be obtained from a rod of length n
// using dynamic programming
int rodCut(vector<int>& price) {
  
    // Set up for unbounded knapsack
    int n = price.size();
    vector<int> len(n);
    for (int i = 0; i < n; ++i)
        len[i] = i + 1;

    // Create a 2D dp array
    vector<vector<int>> dp(n, vector<int>(n + 1, 0));

    // Fill the dp array
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= n; ++j) {
          
            // If we are at the first row, we can 
            // only use the first length piece
            if (i == 0) {
                dp[i][j] = (j / len[0]) * price[0];
            } else {
              
                // Not taking the current length piece
                int notTake = dp[i - 1][j]; 
              
                // Taking the current length piece
                int take = 0;
                if (len[i] <= j) {
                    take = price[i] + dp[i][j - len[i]]; 
                }
              
                // Store the maximum value
                dp[i][j] = max(take, notTake); 
            }
        }
    }

    // The answer is in dp[n-1][n]
    return dp[n - 1][n];
}

// Driver code
int main() {
    vector<int> price = {10, 30, 20};
    cout << rodCut(price) << endl;
    return 0;
}
Java
// Java program for above approach
import java.io.*;

class GFG {

    public static int cutRod(int prices[], int n)
    {

        int mat[][] = new int[n + 1][n + 1];
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                if (i == 0 || j == 0) {
                    mat[i][j] = 0;
                }
                else {
                    if (i == 1) {
                        mat[i][j] = j * prices[i - 1];
                    }
                    else {
                        if (i > j) {
                            mat[i][j] = mat[i - 1][j];
                        }
                        else {
                            mat[i][j] = Math.max(
                                prices[i - 1]
                                    + mat[i][j - i],
                                mat[i - 1][j]);
                        }
                    }
                }
            }
        }

        return mat[n][n];
    }
    public static void main(String[] args)
    {

        int prices[]
            = new int[] { 1, 5, 8, 9, 10, 17, 17, 20 };
        int n = prices.length;

        System.out.println("Maximum obtained value is "
                           + cutRod(prices, n));
    }
}
Python
#  Python program for above approach


def cutRod(prices, n):
    mat = [[0 for i in range(n+1)]for j in range(n+1)]
    for i in range(1, n+1):
        for j in range(1, n+1):
            if i == 1:
                mat[i][j] = j*prices[i-1]
            else:
                if i > j:
                    mat[i][j] = mat[i-1][j]
                else:
                    mat[i][j] = max(prices[i-1]+mat[i][j-i], mat[i-1][j])
    return mat[n][n]


prices = [1, 5, 8, 9, 10, 17, 17, 20]
n = len(prices)

print("Maximum obtained value is ", cutRod(prices, n))

# This Code is Contributed By Vivek Maddeshiya
C#
// C# program for above approach
using System;
using System.Collections;
using System.Collections.Generic;

class GFG {

  public static int cutRod(int[] prices, int n)
  {

    int[, ] mat = new int[n + 1, n + 1];
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        if (i == 0 || j == 0) {
          mat[i, j] = 0;
        }
        else {
          if (i == 1) {
            mat[i, j] = j * prices[i - 1];
          }
          else {
            if (i > j) {
              mat[i, j] = mat[i - 1, j];
            }
            else {
              mat[i, j] = Math.Max(
                prices[i - 1]
                + mat[i, j - i],
                mat[i - 1, j]);
            }
          }
        }
      }
    }

    return mat[n, n];
  }
  public static void Main(string[] args)
  {

    int[] prices
      = new int[] { 1, 5, 8, 9, 10, 17, 17, 20 };
    int n = prices.Length;

    Console.WriteLine("Maximum obtained value is "
                      + cutRod(prices, n));
  }
}

// This code is contributed by Karandeep1234
JavaScript
// Javascript implementation of the above C++ code

function cutRod(prices, n) {
    // Initialize the matrix with all values as 0
    let mat = new Array(n + 1).fill(0).map(() => new Array(n + 1).fill(0));

    // Iterate over the matrix
    for (let i = 0; i <= n; i++) {
        for (let j = 0; j <= n; j++) {
            if (i === 0 || j === 0) {
                mat[i][j] = 0;
            } else {
                if (i === 1) {
                    mat[i][j] = j * prices[i - 1];
                } else {
                    if (i > j) {
                        mat[i][j] = mat[i - 1][j];
                    } else {
                        mat[i][j] = Math.max(prices[i - 1] + mat[i][j - i], mat[i - 1][j]);
                    }
                }
            }
        }
    }

    // Return the last element of the matrix
    return mat[n][n];
}

// Given prices array
let prices = [1, 5, 8, 9, 10, 17, 17, 20];
let n = prices.length;

console.log("Maximum obtained value is " + cutRod(prices, n));

Output
40

Time Complexity: O(n2)
Auxiliary Space: O(n2)

 



Previous Article
Next Article

Similar Reads

Maximum length of rod for Q-th person
Given lengths of n rods in an array a[]. If any person picks any rod, half of the longest rod (or (max + 1) / 2 ) is assigned and remaining part (max - 1) / 2 is put back. It may be assumed that sufficient number of rods are always available, answer M queries given in an array q[] to find the largest length of rod available for qith person, provide
10 min read
Length of longest rod that can fit into a cuboid
Given the length, breadth, and height of a cuboid, the task is to find the length of the longest rod that can fit in a cuboid. Examples: Input: length = 12, breadth = 9, height = 8Output: 17 Input: length = 22, breadth = 19, height = 8Output: 30.1496 Explanation: In the figure GH = length, GF = breadth, FB= height The length of the longest rod is B
5 min read
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts
Given an array arr[] consisting of N positive integers, the task is to find the minimum possible length of a rod that can be cut into N equal parts such that every ith part can be cut into arr[i] equal parts. Examples: Input: arr[] = {1, 2}Output: 4Explanation:Consider the length of the rod as 4. Then it can be divided in 2 equal parts, each having
7 min read
Check if all disks can be placed at a single rod based on given conditions
Given an array arr[] consisting of N integers representing N rods each with a disk of radius arr[i], the task is to check if it is possible to move all disks to a single rod based on the condition that a disk at ith rod can be moved to jth rod only if: abs(i - j) = 1 and ith rod has only a single disk.The disk at ith rod has radii less than the top
6 min read
Determine if possible to get Array by breaking rod segments into two halves
Given a rod of length L and an array arr[] of length N, the task is to find if it is possible to break the rod N-1 times in segments such that length of all the segments is present in the array. Each time a segment of length x can be broken into two parts of length ⌈x/2⌉ and x - ⌈x/2⌉. Examples: Input: L = 1000, N = 1, arr[] = {1000} Output: YESExp
7 min read
Maximise number of cuts in a rod if it can be cut only in given 3 sizes
Given a rod of length N meters, and the rod can be cut in only 3 sizes A , B and C . The task is to maximizes the number of cuts in rod. If it is impossible to make cut then print -1 . Examples: Input: N = 17, A = 10, B = 11, C = 3 Output: 3 Explanation: The maximum cut can be obtain after making 2 cut of length 3 and one cut of length 11. Input: N
7 min read
Probability of cutting a rope into three pieces such that the sides form a triangle
Given a rope. We have to find the probability of cutting a rope into 3 pieces such that they form a triangle. Answer: 0.25 Explanation: Let the length of rope be 1 unit. We choose two points X and Y on the rope. Note: Formation of triangle is based on Triangle inequality i.e. sum of the lengths of any two sides of a triangle must be greater than th
2 min read
Maximum length possible by cutting N given woods into at least K pieces
Given an array wood[] of size N, representing the length of N pieces of wood and an integer K, at least K pieces of the same length need to be cut from the given wooden pieces. The task is to find the maximum possible length of these K wooden pieces that can be obtained. Examples: Input: wood[] = {5, 9, 7}, K = 3 Output: 5 Explanation: Cut arr[0] =
8 min read
Maximum length of all possible K equal length ropes generated by cutting N ropes
Given an array arr[] consisting of N positive integers representing the lengths of N ropes and a positive integer K, the task is to find the maximum length of the rope that has a frequency of at least K by cutting any ropes in any number of pieces. Examples: Input: arr[] = {5, 2, 7, 4, 9}, K = 5Output: 4Explanation:Below are the possible cutting of
8 min read
Cutting Rectangles
Given a rectangle of dimensions L x B find the minimum number (N) of identical squares of the maximum side that can be cut out from that rectangle so that no residue remains in the rectangle. Also, find the dimension K of that square. Examples: Input: L = 2, B = 4Output: N = 2, K = 2Explanation: 2 squares of 2x2 dimension. Input: L = 6, B = 3Output
4 min read
Maximize minimum sweetness in cake cutting
Given that cake consists of N chunks, whose individual sweetness is represented by the sweetness[] array, the task is to cut the cake into K + 1 pieces to maximize the minimum sweetness. Examples: Input: N = 6, K = 2, sweetness[] = {6, 3, 2, 8, 7, 5}Output: 9Explanation: Divide the cake into [6, 3], [2, 8], [7, 5] with sweetness levels 9, 10, and 1
7 min read
Maximizing Profit in Ribbon Cutting with Minimized Costs
Given a collection of ribbons, each of which has a certain length and can be sold at a specific price. However, to sell we need to make sure they are of equal length but there's a cost associated with cutting the ribbons into smaller pieces. The objective is to maximize the total profit by selecting an optimal ribbon length for cutting and making t
8 min read
Number of ways of cutting a Matrix such that atleast one cell is filled in each part
Given an integer K and a matrix mat[][] containing 1 and 0, where 1 denotes the cell is filled and 0 denotes an empty cell. The task is to find the number of ways to cut the matrix into K parts using K-1 cuts such that each part of the matrix contains atleast one filled cell. For each cut, there must be a direction either horizontal or vertical. Th
12 min read
CSES Solutions - Rectangle Cutting
Given an A X B rectangle, your task is to cut it into squares. On each move you can select a rectangle and cut it into two rectangles in such a way that all side lengths remain integers. What is the minimum possible number of moves? Examples: Input: A = 3, B = 5Output: 3Explanation: The three moves required to cut the rectangle into squares are: Cu
8 min read
Maximum Product Cutting | DP-36
Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes product of lengths of all parts. You must make at least one cut. Assume that the length of rope is more than 2 meters.  Examples:  Input: n = 2Output: 1 (Maximum obtainable product is 1*1)Input: n = 3Output: 2 (Maximum obtainable product is 1
15+ min read
Linked List Data Structure
A linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Here’s the comparison of Linked List vs Arrays Linked List: Data Structure: Non-contiguous Memory Allocation:
3 min read
Sorting Algorithms
A Sorting Algorithm is used to rearrange a given array or list of elements in an order. Sorting is provided in library implementation of most of the programming languages. Basics of Sorting Algorithms:Introduction to Sorting Applications of Sorting Sorting Algorithms:Comparison Based : Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Quick
3 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. In this tutorial, we will explore the most commonly used data structures, including arrays, linke
10 min read
Learn Data Structures and Algorithms | DSA Tutorial
Data Structures and Algorithms (DSA) refer to the study of methods for organizing and storing data and the design of procedures (algorithms) for solving problems, which operate on these data structures. DSA is one of the most important skills that every computer science student must have. It is often seen that people with good knowledge of these te
15+ min read
LCM of given array elements
In this article, we will learn how to find the LCM of given array elements. Given an array of n numbers, find the LCM of it. Example: Input : {1, 2, 8, 3}Output : 24Input : {2, 7, 3, 9, 4}Output : 252Method 1 : We know, [Tex]LCM(a, b)=\frac{a*b}{gcd(a, b)} [/Tex]The above relation only holds for two numbers, [Tex]LCM(a, b, c)\neq \frac{a*b*c}{gcd(a
14 min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high. We sort the array using multiple passes. After the first pass, the maximum element goes to end (its cor
8 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted. First we find the smallest element and swap it with the first element. This way we get
8 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Table of Content What is Binary Search Algorithm?Conditions to apply Binary Search Algorithm in a Data St
15+ min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. Table of Content How does QuickSort Algorithm work? Working of Partition Algorithm with IllustrationIllustration of QuickSort Algor
13 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then merging them back together to obtain the sorted array. In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each h
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. Then, you pick a card from the unsorted group and p
10 min read
How to find Shortest Paths from Source to all Vertices using Dijkstra's Algorithm
Given a weighted graph and a source vertex in the graph, find the shortest paths from the source to all the other vertices in the given graph. Note: The given graph does not contain any negative edge. Examples: Input: src = 0, the graph is shown below. Output: 0 4 12 19 21 11 9 8 14 Explanation: The distance from 0 to 1 = 4. The minimum distance fr
15+ min read
Breadth First Search or BFS for a Graph
Breadth First Search (BFS) is a fundamental graph traversal algorithm. It begins with a node, then first traverses all its adjacent. Once all adjacent are visited, then their adjacent are traversed. This is different from DFS in a way that closest vertices are visited before others. We mainly traverse vertices level by level. A lot of popular graph
15+ min read
Depth First Search or DFS for a Graph
Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. Like trees, we traverse all adjacent vertices one by one. When we traverse an adjacent vertex, we completely finish the traversal of all vertices reachable through that adjacent vertex. After we finish traversing one adjacent vertex and its reachable vertices,
15+ min read
Tree Traversal Techniques
Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss about all the tree traversal techniques along with their uses. Table o
15+ min read
Article Tags :
Practice Tags :
three90RightbarBannerImg