Minimum Generations to Equalize Layer Counts

Problem A neural network has n layers, with layer[i] neurons in the i-th layer. In each generation you may add neurons to at most one layer: odd-numbered generations add exactly 1 neuron, even-numbered generations add exactly 2. Find the minimum number of generations needed to make every layer hold an equal neuron count.

Input / Output

  • Input: integer n and array layer of neuron counts.
  • Output: the minimum number of generations required to equalise all layers.

Constraints

  • n up to 10^5; neuron counts vary widely.
  • Neurons can only be added, never removed — so the common target must be at least max(layer).
  • At most one layer may be boosted per generation, and the increment is fixed by the generation's parity.

Example

  • Input: n=4, layer=[1,1,2,4] → Output: 6. Deficits against target 4 are [3,3,2,0], total 8; generations 1..6 supply increments 1,2,1,2,1,2 = 9, and the deficits can be packed into them.
  • Tricky case: all layers already equal → 0 generations. A single deficit of 1 needs an odd generation, so a lone deficit of exactly 1 costs 1 generation, not the naive "total/2".
asked …
LeaderboardSalaryAccount