Left and Right Sum Differences
Description:
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.
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].
Approaches:
1. Optimal Prefix Sum Approach
To avoid calculating the left and right sums from scratch for every element (which would result in a less optimal time complexity), we can utilize a running sum strategy:
- Calculate the
totalSumof all elements in the array. - Initialize a
leftSumvariable to 0. - Iterate through the array. For any element at index
i, therightSumcan be deduced dynamically using the formula:rightSum = totalSum - leftSum - nums[i]. - Calculate the absolute difference, add it to our answer array, and update the
leftSumby addingnums[i]before moving to the next iteration.
- Time Complexity: because we iterate through the
numsarray twice (once to find the total sum, and once to calculate the differences). - Space Complexity: auxiliary space. We use an output array to store the results, which is generally not counted towards auxiliary space complexity. The space used for tracking sums is constant.
Optimal Solutions:
C++
class Solution {
public:
vector<int> leftRightDifference(vector<int>& nums) {
int totalSum = 0;
for (int num : nums) {
totalSum += num;
}
int leftSum = 0;
vector<int> ans(nums.size());
for (size_t i = 0; i < nums.size(); i++) {
int rightSum = totalSum - leftSum - nums[i];
ans[i] = abs(leftSum - rightSum);
leftSum += nums[i];
}
return ans;
}
};
Java
class Solution {
public int[] leftRightDifference(int[] nums) {
int totalSum = 0;
for (int num : nums) {
totalSum += num;
}
int leftSum = 0;
int[] ans = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
int rightSum = totalSum - leftSum - nums[i];
ans[i] = Math.abs(leftSum - rightSum);
leftSum += nums[i];
}
return ans;
}
}
Python
class Solution:
def leftRightDifference(self, nums: list[int]) -> list[int]:
total_sum = sum(nums)
left_sum = 0
ans = []
for num in nums:
right_sum = total_sum - left_sum - num
ans.append(abs(left_sum - right_sum))
left_sum += num
return ans
JavaScript
/**
* @param {number[]} nums
* @return {number[]}
*/
const leftRightDifference = function(nums) {
let totalSum = nums.reduce((acc, curr) => acc + curr, 0);
let leftSum = 0;
let ans = new Array(nums.length);
for (let i = 0; i < nums.length; i++) {
let rightSum = totalSum - leftSum - nums[i];
ans[i] = Math.abs(leftSum - rightSum);
leftSum += nums[i];
}
return ans;
};
Finished reading? Mark this topic as complete.