Quant Memo
Coding/●●●●●

Max of the array except self

Given an integer array nums, return an array out where out[i] is the maximum of every element except nums[i].

nums = [3, 1, 4, 2]  ->  [4, 4, 3, 4]

Solve it in O(n)O(n) time. Note the trap: unlike sums or products, you cannot keep one running total and "remove" nums[i] from it.

Show a hint

The max over everything except i is the larger of (max of everything to its left) and (max of everything to its right). Can you compute both directions in two sweeps?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions