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 index i in the array nums. If there is no such element, leftSum[i] = 0.rightSum[i] is the sum of elements to the right of the index i in the array nums. 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].
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 105Problem Overview: Given an integer array nums, compute a new array where each index contains the absolute difference between the sum of elements to its left and the sum of elements to its right. For index i, calculate |leftSum - rightSum|. The first element has no left values and the last element has no right values.
Approach 1: Brute Force Calculation (Time: O(n²), Space: O(1))
For each index i, iterate through the array twice: once to sum all elements before i and once to sum all elements after i. Compute abs(leftSum - rightSum) and store the result. This approach directly follows the problem definition and requires no additional data structures. However, because every index performs two scans of the array, the total work becomes quadratic.
This method works for small inputs or when you want a straightforward baseline implementation. It also helps verify correctness before optimizing.
Approach 2: Optimized Prefix Sum Calculation (Time: O(n), Space: O(1))
The key observation: once you know the total sum of the array, you can compute the right sum without scanning again. Start by calculating the total sum. Then iterate through the array while maintaining a running leftSum. For index i, subtract nums[i] from the remaining total to get rightSum. Now compute abs(leftSum - rightSum). After that, update leftSum += nums[i].
This technique uses the idea behind prefix sums: cumulative sums allow constant‑time range calculations. Only one pass through the array is required after computing the total sum, giving linear performance.
Recommended for interviews: The prefix sum approach is the expected solution. It reduces the complexity from O(n²) to O(n) while using constant extra space. Mentioning the brute force approach first shows you understand the raw problem definition, but implementing the optimized prefix-sum pass demonstrates algorithmic awareness and efficient array processing.
This approach involves calculating the left and right sums independently for each index, resulting in an O(n^2) time complexity. Though this is not the most efficient method, it helps understand the problem.
The code above calculates the left and right sums for each index in a brute force manner. It uses two nested loops to sum the elements to the left and right of each current index.
Time Complexity: O(n^2)
Space Complexity: O(1) (excluding the space required for the output array 'answer')
This approach reduces the time complexity by using prefix sums. We precompute the total sum of the array and use it to efficiently find the left and right sums for each index.
This solution computes the total sum of the array first. Then, for each index, it uses the total sum and the left sum (tracked dynamically) to calculate right sums more efficiently.
Time Complexity: O(n)
Space Complexity: O(1) (if we don't count the space for the answer array)
We define a variable l to represent the sum of elements to the left of index i in the array nums, and a variable r to represent the sum of elements to the right of index i in the array nums. Initially, l = 0, r = sum_{i = 0}^{n - 1} nums[i].
We traverse the array nums. For the current number x, we update r = r - x. At this point, l and r represent the sum of elements to the left and right of index i in the array nums, respectively. We add the absolute difference of l and r to the answer array ans, then update l = l + x.
After the traversal, we return the answer array ans.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1), not counting the space for the return value.
Similar problems:
| Approach | Complexity |
|---|---|
| Brute Force Calculation | Time Complexity: O(n^2) |
| Optimized Prefix Sum Calculation | Time Complexity: O(n) |
| Prefix Sum | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Calculation | O(n²) | O(1) | Useful for understanding the problem or verifying correctness on small arrays. |
| Optimized Prefix Sum Calculation | O(n) | O(1) | Best general solution for interviews and large inputs. |
2574. Left and Right Sum Differences - JAVA - Weekly Contest 334 (Detailed explanation + coding) • Sourin Majumdar • 4,870 views views
Watch 9 more video solutions →Practice Left and Right Sum Differences with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor