DDSA Solutions

Find maximum volume of a cuboid

Advertisement

Intuition

Given constraint sum of dimensions = k, maximize volume l*w*h. By AM-GM inequality, cube maximizes volume.

Algorithm

  1. 1For integer dimensions: floor(k/3)^2 * ceil(k/3) adjusted for remainder. Try (q,q,q), (q,q,q+1), (q,q+1,q+1).

Common Pitfalls

  • AM-GM: equal dimensions maximize product. For integer constraints: check two or three candidate triples.
Find maximum volume of a cuboid.java
Java
// Approach: Sort dimensions. Maximize length*width*height under constraints using sorting and greedy pairing.
// Time: O(n log n) Space: O(1)
class Solution {

    double maxVolume(double perimeter, double area) {
        double l = (perimeter - Math.sqrt(Math.pow(perimeter, 2) - 24 * area)) / 12;
        double h = (perimeter / 4) - 2 * l;
        
        return l * l * h;
    }
}
Advertisement
Was this solution helpful?