VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximize-the-number-of-segments-of-length-p-q-and-r/

⇱ Maximize the number of segments of length x, y and z - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximize the number of segments of length x, y and z

Last Updated : 11 Jul, 2025

Given a rod of length n, the task is to cut the rod in such a way that the total number of segments of length x, y, and z is maximized. The segments can only be of length x, y, and z. 
Note: If no segment can be cut then return 0.

Examples: 

Input: n = 4, x = 2, y = 1, z = 1
Output: 4
Explanation: Total length is 4, and the cut lengths are 2, 1 and 1.  We can make maximum 4 segments each of length 1.

Input: n = 5, x = 5, y = 3, z = 2
Output: 2
Explanation: Here total length is 5, and the cut lengths are 5, 3 and 2. We can make two segments of lengths 3 and 2.

Input: n = 7, x = 8, y = 9, z = 10
Output: 0
Explanation: Here the total length is 7, and the cut lengths are 8, 9, and 10. We cannot cut the segment into lengths that fully utilize the segment, so the output is 0.

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

For the recursive approach, the function explores all possible ways to cut the rod into segments of given lengths. At each step, it considers three options:

  • Reduce the length by the first segment length (x).
  • Reduce the length by the second segment length (y).
  • Reduce the length by the third segment length (z).

The recurrence relation for the problem is as follows:

if n < 0
maxCutHelper(n, x, y, z) = -1,

else
maxCutHelper(n, x, y, z) = 1 + max(maxCutHelper(n - x, x, y, z), maxCutHelper(n - y, x, y, z), maxCutHelper(n - z, x, y, z))

Base Cases:

  • If the rod length is zero (n = 0), return 0 (no more cuts can be made).
  • If the rod length becomes negative (n < 0), return -1 (invalid result).

Output
5

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

The recursive approach is optimized using memoization, storing previously computed results in a memo array to avoid redundant computations. The two key properties of dynamic programming are utilized:

1. Optimal Substructure: The solution for maximizing cuts can be derived from smaller subproblems:

  • If the rod length n is zero, the number of cuts is zero.
  • If the rod length becomes negative, the result is invalid (-1).
  • Otherwise, for each segment length (x, y, z), the solution is the maximum result of reducing the rod by that segment length and recursively solving the subproblem.

2. Overlapping Subproblems: The function repeatedly computes results for the same rod length (n) with the same segment lengths. Using a memoization array memo, where memo[n] stores the result for rod length n, avoids redundant calculations.

  • Initialize a memoization array memo of size n + 1 with -1.
  • In the recursive function, check if the result for the current rod length (n) is already computed (memo[n] != -1). If so, return the stored result.
  • Otherwise, compute the result using the recurrence relation, and store it in memo[n].

Output
5

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

The approach is similar to the previous one, but instead of breaking down the problem recursively, we iteratively build up the solution by calculating in a bottom-up manner. So, we will create a 1D array dp of size (n + 1) of type integer. The state dp[i] will store the maximum number of cuts possible for a rod of length i.

The dynamic programming relation is as follows:

  • If the current rod length i is less than the segment length x: dp[i] = dp[i]
  • Otherwise, we update dp[i] as: dp[i] = max(dp[i], dp[i - k] + 1), where k can be x, y, or z.

This means that if the current rod length is smaller than the segment length, no cut is possible for this length, and we keep the previous value. Otherwise, we check if adding a segment of length k results in a greater number of cuts and update the value of dp[i] accordingly. Finally, if no valid cuts are possible for the rod of length n, we return 0. Otherwise, the value at dp[n] will give the maximum number of cuts.


Output
5
Comment