You are given a 0-indexed integer array nums
of size n
.
Define two arrays leftSum
and rightSum
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 an integer array answer
of size n
where answer[i] = |leftSum[i] - rightSum[i]|
.
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 <= 1000
1 <= nums[i] <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The straightforward approach is to examine every number in the list one by one. For each number, we independently calculate the total of everything to its left and the total of everything to its right, then find the difference.
Here's how the algorithm would work step-by-step:
def left_and_right_sum_differences_brute_force(input_numbers):
# This collection will hold the final calculated differences for each position.
answer_array = []
# We must process each number individually to compute its unique left and right sums.
for current_index in range(len(input_numbers)):
# Slicing the list isolates the elements to the left and right of the current number.
left_side_numbers = input_numbers[0:current_index]
left_sum = sum(left_side_numbers)
right_side_numbers = input_numbers[current_index + 1:]
right_sum = sum(right_side_numbers)
# The requirement is for a positive value, so we take the absolute difference.
absolute_difference = abs(left_sum - right_sum)
answer_array.append(absolute_difference)
return answer_array
The most efficient way to solve this is to avoid recalculating sums from scratch for every single number. Instead, we can cleverly build up the sums for the left and right sides by making two passes over the collection of numbers, one from each direction.
Here's how the algorithm would work step-by-step:
def left_and_right_sum_differences(input_numbers: list[int]) -> list[int]:
number_of_elements = len(input_numbers)
answer_array = [0] * number_of_elements
# This first pass populates our temporary list with the sum of all numbers to the left.
left_running_sum = 0
for index in range(number_of_elements):
answer_array[index] = left_running_sum
left_running_sum += input_numbers[index]
# The second pass calculates right-side sums while simultaneously finding the final difference.
right_running_sum = 0
for index in range(number_of_elements - 1, -1, -1):
# We can now calculate the final value because we have the left sum and the right sum.
answer_array[index] = abs(answer_array[index] - right_running_sum)
right_running_sum += input_numbers[index]
return answer_array
Case | How to Handle |
---|---|
Input array with a single element | The solution correctly produces an output array [0] as both the left and right sums are zero by definition. |
Calculation for the first element at index 0 | The algorithm correctly handles this boundary by ensuring the leftSum for the first element is always zero. |
Calculation for the last element at index n-1 | The algorithm correctly handles this boundary by ensuring the rightSum for the last element is always zero. |
Maximum size input (n=1000) affecting performance | An efficient O(n) linear-time solution is required to avoid a timeout that a naive O(n^2) approach would cause. |
Potential for integer overflow when calculating sums | The solution must confirm that the maximum possible sum, around 10^8, safely fits within a standard 32-bit signed integer. |
Input array containing all identical values | The solution's logic is independent of the input value distribution and correctly processes arrays with uniform numbers. |
Input values being strictly positive integers | The solution can rely on the problem's constraint that numbers are positive, which means prefix sums will be monotonically increasing. |
Space complexity for storing intermediate sums | An optimal solution uses O(1) extra space by calculating sums on-the-fly and reusing the output array, avoiding explicit `leftSum` and `rightSum` arrays. |