Why it works
- For every i, left + nums[i] + right = total. So right = total − left − nums[i]: one sum up front gives the right side at every index without adding it up again.
- left grows by one number per step, so it is the running prefix sum, and no stretch is added up twice.
- The check comes before nums[i] joins left, so the number at i counts on neither side. Indices are tried from left to right, so the first match is the leftmost pivot.
Complexity
Time O(n): one pass for the total and one for the pivot. Memory O(1): only total and left, no array of prefix sums.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗