The score formula values[i] + values[j] + i - j rewards spots with high values but penalizes the distance between them. Two high-value spots on opposite ends of the array can score lower than two moderate spots next to each other.
We need to find the pair (i, j) with i < j that maximizes this score. Checking every pair gives a correct but quadratic solution. The formula can also be regrouped so that each index contributes its own independent term, which reduces the problem to a single pass with a running maximum.
2 <= values.length <= 5 * 10^4: an O(n^2) scan checks about 1.25 billion pairs at the upper bound, which is too slow. The target is O(n).1 <= values[i] <= 1000: every adjacent pair scores values[i] + values[i+1] - 1 >= 1, so the maximum score is always positive and initializing the answer to 0 is safe. Values cap at 1000 and indices at 5 * 10^4, so every intermediate sum fits in a 32-bit integer.Try every pair (i, j) where i < j, compute values[i] + values[j] + i - j, and keep the largest result. Every valid pair gets checked, so correctness is immediate. The cost is the quadratic number of pairs.
maxScore to 0i from 0 to n-2:j from i+1 to n-1:values[i] + values[j] + i - jmaxScore if this score is largermaxScoremaxScore regardless of input size.For each index j, the inner loop rescans every previous index i to find its best left partner, even though that best partner changes only when a new element beats the current one. Carrying the best left partner forward in a single variable removes the inner loop entirely.
The score formula mixes the two indices:
values[i] + values[j] + i - j
Regrouping the terms by index separates them:
(values[i] + i) + (values[j] - j)
The score is now the sum of two independent parts. The first part, values[i] + i, depends only on the left spot. The second part, values[j] - j, depends only on the right spot. For any fixed j, maximizing the total score means maximizing values[i] + i among all i < j.
A single left-to-right scan handles that: maintain a running maximum of values[i] + i (the best left partner seen so far). At each position j, combine that running max with values[j] - j to get the best score ending at j, then check whether j itself becomes the new best left partner. The same scan-with-best-earlier-candidate pattern solves Best Time to Buy and Sell Stock.
The greedy choice is safe because no past decision ever needs to be revisited. If index k has a higher values[k] + k than every earlier index, then k is a better left partner for every future j, regardless of what values[j] - j turns out to be. The running maximum therefore always holds the optimal left partner for the current position, and evaluating it at every j covers all valid pairs.
maxLeftScore to values[0] + 0 (the values[i] + i contribution of the first spot)maxScore to 0j from 1 to n-1:maxLeftScore + values[j] - jmaxScore if this is bettermaxLeftScore if values[j] + j is larger (this position becomes the new best left partner for future j's)maxScoremaxLeftScore and maxScore) regardless of input size.