Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
More than 2 occurrences of -1 can be ignored.
We can add the first positive number to the beginning and the last positive number to the end so that any consecutive of -1s are surrounded by positive numbers.
Suppose the answer is <code>d</code>, it can be proved that for the optimal case we'll replace -1s with values <code>0 < x <= y</code> and it's always optimal to select <code>x = min(a) + d</code>. So we only need to select <code>y</code>.
Binary search on <code>d</code>.