2574. Left and Right Sum Differences LeetCode Solution
In this guide, you will get 2574. Left and Right Sum Differences LeetCode Solution with the best time and space complexity. The solution to Left and Right Sum Differences problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Left and Right Sum Differences solution in C++
- Left and Right Sum Differences solution in Java
- Left and Right Sum Differences solution in Python
- Additional Resources

Problem Statement of Left and Right Sum Differences
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2574. Left and Right Sum Differences LeetCode Solution in C++
class Solution {
public:
vector<int> leftRigthDifference(vector<int>& nums) {
const int n = nums.size();
vector<int> ans(n);
vector<int> leftSum(n);
vector<int> rightSum(n);
int prefix = 0;
int suffix = 0;
for (int i = 0; i < n; ++i) {
if (i > 0)
prefix += nums[i - 1];
leftSum[i] = prefix;
}
for (int i = n - 1; i >= 0; --i) {
if (i + 1 < n)
suffix += nums[i + 1];
rightSum[i] = suffix;
}
for (int i = 0; i < n; ++i)
ans[i] = abs(leftSum[i] - rightSum[i]);
return ans;
}
};
/* code provided by PROGIEZ */
2574. Left and Right Sum Differences LeetCode Solution in Java
class Solution {
public int[] leftRigthDifference(int[] nums) {
final int n = nums.length;
int[] ans = new int[n];
int[] leftSum = new int[n];
int[] rightSum = new int[n];
int prefix = 0;
int suffix = 0;
for (int i = 0; i < n; ++i) {
if (i > 0)
prefix += nums[i - 1];
leftSum[i] = prefix;
}
for (int i = n - 1; i >= 0; --i) {
if (i + 1 < n)
suffix += nums[i + 1];
rightSum[i] = suffix;
}
for (int i = 0; i < n; ++i)
ans[i] = Math.abs(leftSum[i] - rightSum[i]);
return ans;
}
}
// code provided by PROGIEZ
2574. Left and Right Sum Differences LeetCode Solution in Python
class Solution:
def leftRigthDifference(self, nums: list[int]) -> list[int]:
n = len(nums)
leftSum = [0] * n
rightSum = [0] * n
prefix = 0
suffix = 0
for i in range(n):
if i > 0:
prefix += nums[i - 1]
leftSum[i] = prefix
for i in range(n - 1, -1, -1):
if i + 1 < n:
suffix += nums[i + 1]
rightSum[i] = suffix
return [abs(l - r) for l, r in zip(leftSum, rightSum)]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.