Left and Right Sum Differences
The task requires us to find the difference between the sum of the elements to the left and right of each index for a given array. We can first calculate cumulative sums from left to right and right to left. Then, we can iterate over these arrays and find the absolute difference for each index.
Python solution:


In this solution, we start by initializing three lists, leftSum
, rightSum
, and answer
, of size n
(the size of the input array). Then, we calculate the cumulative sum from the left and from the right, excluding the current index. Finally, we calculate the absolute difference between the cumulative sums at each index and store it in the answer
list. The space complexity of this solution is O(n) due to the additional lists, and the time complexity is O(n) because we are iterating over the list a constant number of times.
This solution is using the concept of prefix sum.
The prefix sum array is a common technique used in arraybased problems, which involves creating an extra array that stores the sum of elements of the original array up to a certain index.
In our solution, leftSum[i]
is a prefix sum that stores the sum of all elements to the left of i
in the original array nums
. Similarly, rightSum[i]
is a kind of reverse prefix sum that stores the sum of all elements to the right of i
in nums
.
By precomputing these prefix sums, we are able to quickly calculate the sum of elements to the left and right of any index in constant time, making our algorithm more efficient.
Besides the Prefix Sum technique, this problem is a clear example of the twopointers strategy as well. In fact, to generate the leftSum
and rightSum
arrays, you can use two pointers: one starting from the beginning of the nums
array (for leftSum
), and the other one starting from the end of the nums
array (for rightSum
).
In addition, the problem applies the concept of absolute difference, which requires understanding of basic arithmetic and the Python builtin function abs()
.
So, to sum up, the problem incorporates these concepts:
 Prefix Sum: to calculate cumulative sums.
 Twopointers: to traverse the array from both ends.
 Absolute Difference: to calculate the absolute difference between left and right sums.
 Array Manipulation: to work with Python lists, generate new lists, and access elements by index.
The rightSum
array is effectively a Suffix Sum array. A Suffix Sum array is an array derived from another array such that each element at index i
is the sum of all the elements from index i
to the end of the original array. This is the opposite of a Prefix Sum array where each element is the sum of all elements from the start of the array to index i
.
So, in addition to Prefix Sum and the other concepts I mentioned, the problem indeed incorporates Suffix Sum concept as well. Thanks for pointing this out.
The Suffix Sum can be calculated in a similar way to the Prefix Sum but starting from the end of the nums
array and moving towards the beginning of the array.
Abstract Representation of the Problem
The problem can be represented in an abstract way as follows:
Given an array of integers nums
, we need to generate an output array answer
where each element at an index i
is the absolute difference between the sum of all elements to the left of i
(excluding i
) and the sum of all elements to the right of i
(excluding i
) in nums
.
In the context of prefix and suffix sums, we can generate two arrays:
leftSum
array whereleftSum[i]
is the sum of all elements from the start of the array up to but not including the element at indexi
.rightSum
array whererightSum[i]
is the sum of all elements from the end of the array down to but not including the element at indexi
.
The desired output answer
array is then calculated as the absolute difference between corresponding elements of the leftSum
and rightSum
arrays.
This problem showcases the utility of prefix and suffix sums in efficiently calculating cumulative sums over subarrays of a given array. It also highlights the use of absolute difference as a measure of “distance” or “discrepancy” between two quantities.