Program to find maximum profit after cutting rods and selling same length rods in Python

Suppose we have a list of rod lengths called rodLen. We also have two integers called profit and cost, representing profit per length and cost per cut. We can gain profit per unit length of a rod but we can only sell rods that are all of the same length. We can also cut a rod into two pieces such that their lengths are integers, but we have to pay cost amount for each cut. We can cut a rod as many times as we want. We have to find the maximum profit that we can make.

So, if the input is like rodLen = [7, 10], profit = 6, cost = 4, then the output will be 82. We can cut the rod of length 7 into two rods with lengths 5 and 2. We can then cut the rod of length 10 into two rods, both with length 5. Then sell all 3 rods of length 5 for a total profit of (5 + 5 + 5) * 6 - (2 * 4) = 82.

Algorithm

To solve this, we will follow these steps ?

  • n := size of rodLen
  • if n is same as 0, then
    • return 0
  • l_max := maximum of rodLen
  • p_max := 0
  • for cuts in range 1 to l_max, do
    • p_cut := 0
    • for each rod_len in rodLen, do
      • if rod_len < cuts, then
        • go for next iteration
      • c_count := rod_len / cuts
      • total_len := c_count * cuts
      • if rod_len is same as total_len, then
        • c_count := c_count - 1
      • curr_profit := total_len * profit - cost * c_count
      • if curr_profit < 0, then
        • go for next iteration
      • p_cut := p_cut + curr_profit
    • p_max := maximum of p_max and p_cut
  • return p_max

Example

Let us see the following implementation to get better understanding ?

def solve(rodLen, profit, cost):
    n = len(rodLen)
    if n == 0:
        return 0
    l_max = max(rodLen)
    p_max = 0

    for cuts in range(1, l_max + 1):
        p_cut = 0
        for rod_len in rodLen:
            if rod_len < cuts:
                continue
            c_count = rod_len // cuts
            total_len = c_count * cuts
            if rod_len == total_len:
                c_count -= 1
            curr_profit = total_len * profit - cost * c_count
            if curr_profit < 0:
                continue
            p_cut += curr_profit
        p_max = max(p_max, p_cut)
    return p_max

rodLen = [7, 10]
profit = 6
cost = 4
print(solve(rodLen, profit, cost))

The output of the above code is ?

82

How It Works

The algorithm tries all possible cut lengths from 1 to the maximum rod length. For each cut length, it calculates how many pieces can be obtained from each rod and the associated profit after deducting cutting costs. The key insight is that we need c_count - 1 cuts to get c_count pieces from a rod.

Conclusion

This dynamic programming approach finds the optimal cutting strategy by testing all possible uniform lengths. The solution has O(n × max_length) time complexity where n is the number of rods.

Updated on: 2026-03-26T17:03:23+05:30

561 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements