Left and Right Sum Differences
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Given a 0-indexed integer array nums, find a 0-indexed integer array
answer where:
answer.length == nums.length.answer[i] = |leftSum[i] - rightSum[i]|.
Where:
leftSum[i]is the sum of elements to the left of the indexiin the arraynums. If there is no such element,leftSum[i] = 0.rightSum[i]is the sum of elements to the right of the indexiin the arraynums. If there is no such element,rightSum[i] = 0.
Return the array answer.
Examples
Example 1
Input: nums = [10,4,8,3]
Output: [15,1,11,22]
Explanation: The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].
The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].
Example 2
Input: nums = [1]
Output: [0]
Explanation: The array leftSum is [0] and the array rightSum is [0].
The array answer is [|0 - 0|] = [0].
Constraints
1 <= nums.length <= 10001 <= nums[i] <= 10^5
Solution
Method 1 – Prefix Sums
Intuition
To efficiently compute the left and right sums for each index, we can use prefix sums. By precomputing the total sum and iterating through the array, we can update left and right sums in a single pass.
Approach
- Compute the total sum of the array.
- Initialize
left_sumas 0. - For each index
i, the right sum istotal_sum - left_sum - nums[i]. - Compute
|left_sum - right_sum|for each index and updateleft_sumby addingnums[i]. - Return the answer array.
Code
C++
class Solution {
public:
vector<int> leftRightDifference(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
int total = 0, left = 0;
for (int x : nums) total += x;
for (int i = 0; i < n; ++i) {
int right = total - left - nums[i];
ans[i] = abs(left - right);
left += nums[i];
}
return ans;
}
};
Go
func leftRightDifference(nums []int) []int {
n := len(nums)
ans := make([]int, n)
total, left := 0, 0
for _, x := range nums { total += x }
for i, x := range nums {
right := total - left - x
ans[i] = abs(left - right)
left += x
}
return ans
}
func abs(x int) int { if x < 0 { return -x } else { return x } }
Java
class Solution {
public int[] leftRightDifference(int[] nums) {
int n = nums.length, total = 0, left = 0;
int[] ans = new int[n];
for (int x : nums) total += x;
for (int i = 0; i < n; i++) {
int right = total - left - nums[i];
ans[i] = Math.abs(left - right);
left += nums[i];
}
return ans;
}
}
Kotlin
class Solution {
fun leftRightDifference(nums: IntArray): IntArray {
val n = nums.size
val ans = IntArray(n)
var total = nums.sum()
var left = 0
for (i in nums.indices) {
val right = total - left - nums[i]
ans[i] = kotlin.math.abs(left - right)
left += nums[i]
}
return ans
}
}
Python
class Solution:
def leftRightDifference(self, nums: list[int]) -> list[int]:
n = len(nums)
ans = [0] * n
total = sum(nums)
left = 0
for i, x in enumerate(nums):
right = total - left - x
ans[i] = abs(left - right)
left += x
return ans
Rust
impl Solution {
pub fn left_right_difference(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut ans = vec![0; n];
let total: i32 = nums.iter().sum();
let mut left = 0;
for (i, &x) in nums.iter().enumerate() {
let right = total - left - x;
ans[i] = (left - right).abs();
left += x;
}
ans
}
}
TypeScript
class Solution {
leftRightDifference(nums: number[]): number[] {
const n = nums.length;
const ans = new Array(n).fill(0);
let total = nums.reduce((a, b) => a + b, 0);
let left = 0;
for (let i = 0; i < n; i++) {
const right = total - left - nums[i];
ans[i] = Math.abs(left - right);
left += nums[i];
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of nums. We traverse the array twice (sum and answer). - 🧺 Space complexity:
O(n), for the answer array.