VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cutting-a-rod-dp-13/

⇱ Rod Cutting - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Rod Cutting

Last Updated : 9 Mar, 2026

Given a rod of length n and an array price[]. price[i] denotes the price of a piece of length i. Determine the maximum amount obtained by cutting the rod into pieces and selling the pieces.

Note: price[0] is always 0.

Input: price[] = [0, 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.

Input : price[] = [0, 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*price[1]= 8*3 = 24.

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

Using Recursion - O(2^n) Time and O(n) Space

  • For a rod of length i, try all possible cuts j (1 ≤ j ≤ i).
  • For each cut, add the price of length j with the best profit of remaining rod (i − j).
  • Take the maximum profit among all possible cuts.

Output
22

Using the idea of Unbounded Knapsack - O(n^2) time and O(n^2) space

  • This problem can be treated like an Unbounded Knapsack, where each cut length can be used multiple times.
  • For each cut length, we have two choices: take the cut (if it fits) or skip it.
  • We choose the maximum profit from these choices to get the best way to cut the rod.

Output
22

Using Top-Down DP (Memoization) - O(n^2) Time and O(n) Space

  • In recursion, the same rod lengths are solved multiple times.
  • Since there are only n possible rod lengths, we store their results in a DP array.
  • If a result is already stored, we reuse it instead of recomputing, improving efficiency.

Output
22

Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n) Space

  • We compute the maximum profit starting from smaller rod lengths and move to larger ones.
  • For a rod of length i, we try all cuts j and (i − j).
  • Since smaller lengths are already solved, we reuse their results to fill the DP table.

Output
22
Comment
Article Tags: