2714. Left and Right Sum Differences
My accepted JavaScript solution to LeetCode problem 2714, Left and Right Sum Differences, running in 4ms.
- Difficulty: Easy
- JavaScript
- Runtime 4ms
- Memory 58.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 4ms, memory 58.4MB, accepted 2025-12-29.
/**
* @param {number[]} nums
* @return {number[]}
*/
var leftRightDifference = function(nums) {
const n = nums.length;
const result = new Array(n);
let leftSum = 0, rightSum = nums.reduce((a, b) => a + b, 0);
for (let i = 0; i < n; i++) {
rightSum -= nums[i];
result[i] = Math.abs(leftSum - rightSum);
leftSum += nums[i];
}
return result;
};