# Herb and Bjarne

See the original problem on HackerRank.

Herb puts Bjarne’s birthday present in a cuboid box. The dimensions of its edges are positive integers and the sum of its length, height, and width is N. ### Input Format

A single integer, N (the sum of the box’s length, height, and width).

### Constraints

$$3 \leq N \leq 10^3$$

### Output Format

Print the maximum possible volume of the box.

## Solutions

The product of edge lengths is maximized when the lenghts are as long as possible.That is, we have to evenly distribute the available lenght through the edges and then give the possible remainder two one or two edges (the remainder is at most 2 because we divde by 3).

We start by setting all edges to $$N/3$$ and then we just distribute the remainder evenly:

  1 2 3 4 5 6 7 8 9 10 11  int n; cin >> n; array nums; nums.fill(n / 3); auto remainder = n%3; auto volume = 1; for (auto i=0; i<3; ++i) { volume *= nums[i] + (remainder-- > 0); } cout << volume; 

The same can be implemented with reduce pattern:

 1 2 3 4 5 6 7 8  int n; cin >> n; array nums; nums.fill(n / 3); auto remainder = n%3; cout << std::accumulate(begin(nums), end(nums), 1, [&](int curr, int nxt){ return curr * (nxt + (remainder-- > 0)); }) << endl; 
We've worked on this challenge in these gyms: modena