Opening the reading…
Opening the reading…
PREFIX SUM › PREFIX SUM
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →At index i, the value itself belongs to neither sum. The left sum contains nums[0] through nums[i - 1], while the right sum contains nums[i + 1] through nums[n - 1]. Once those two totals are known, the required answer is simply their absolute difference. The first and last positions naturally have an empty side, whose sum is zero.
Adjacent indices share almost all of their elements, so recomputing both sides from scratch wastes that overlap. A forward pass can carry the sum of elements already passed, and a backward pass can carry the sum of elements still to come. Storing those results lets a final pass apply the same absolute-difference rule at every position without repeating any additions.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n) | The three loops each visit every index at most once. Their work is sequential rather than nested, so the total number of additions and comparisons is a constant multiple of n. |
| Space | O(n) extra | The leftSum, rightSum, and answer arrays each hold at most n values. The returned answer is required output and is excluded from the extra-space count; the working arrays still use O(n), which is also the worst case when the input has n elements. |
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> leftRightDifference(vector<int>& nums) {
int n = nums.size();
vector<int> leftSum(n, 0), rightSum(n, 0);
for (int i = 1; i < n; ++i) {
leftSum[i] = leftSum[i - 1] + nums[i - 1];
}
for (int i = n - 2; i >= 0; --i) {
rightSum[i] = rightSum[i + 1] + nums[i + 1];
}
vector<int> answer(n);
for (int i = 0; i < n; ++i) {
answer[i] = abs(leftSum[i] - rightSum[i]);
}
return answer;
}
};The offsets in both passes are the key detail. leftSum[i] starts from leftSum[i - 1] and adds nums[i - 1], so nums[i] is excluded. The backward pass mirrors that rule: rightSum[i] starts from rightSum[i + 1] and adds nums[i + 1]. Those exclusions are exactly what make the current element belong to neither side.
You can avoid both sum arrays by computing the total sum first. At index i, subtracting nums[i] from the remaining total leaves the sum on the right; the variable left then holds the sum on the left. Compute the answer from those two values, and only afterward add nums[i] to left. This is an optimization in space, not a different result or a faster asymptotic algorithm.
#include <cstdlib>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> leftRightDifference(vector<int>& nums) {
int total = 0;
for (int value : nums) {
total += value;
}
vector<int> answer(nums.size());
int left = 0;
for (int i = 0; i < static_cast<int>(nums.size()); ++i) {
int right = total - left - nums[i];
answer[i] = abs(left - right);
left += nums[i];
}
return answer;
}
};