Company: Cognizant ace_13may
Difficulty: medium
A construction company owner has surplus metal rods of arbitrary lengths. A contractor will buy rods of the same exact integer length ( saleLength ). Rods can be cut to increase the number of sellable rods, but each cut costs costPerCut . Any leftover rods not matching saleLength are discarded. The total profit is calculated as: totalProfit = totalUniformRods × saleLength × salePrice - totalCuts × costPerCut Find the saleLength that maximizes profit. Example lengths = [30, 59, 110] costPerCut = 1 salePrice = 10 Testing two possible lengths: For saleLength = 30: Rod 30: No cuts needed → 1 piece Rod 59: Cut off 29 units (1 cut) → 1 piece Rod 110: Cut off 20 units + 2 more cuts → 3 pieces Total: 5 pieces, 4 cuts Revenue = (10 × 5 × 30) - (4 × 1) = 1496 For saleLength = 5: Rod 30: 5 cuts → 6 pieces Rod 59: Cut off 4 units (1 cut) + 10 more cuts → 11 pieces Rod 110: 21 cuts → 22 pieces Total: 39 pieces, 37 cuts Revenue = (10