Left and Right Sum Differences

Given a 0-indexed integer array nums, return an array answer of the same length where answer[i] = |leftSum[i] - rightSum[i]|. leftSum[i] is the sum of the elements to the left of index i, and rightSum[i] is the sum of the elements to the right of index i. A side with no elements sums to 0.

30:00

Left and Right Sum Differences
easy
Free written solution
Topics
Companies

Given a 0-indexed integer array nums, return an array answer of the same length where answer[i] = |leftSum[i] - rightSum[i]|. leftSum[i] is the sum of the elements to the left of index i, and rightSum[i] is the sum of the elements to the right of index i. A side with no elements sums to 0.

Example 1:
Input: {"nums":[10,4,8,3]}
Output: [15,1,11,22]
Constraints:
  • 1≤nums.length≤10001 \leq \text{nums.length} \leq 1000

  • 1≤nums[i]≤1051 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[10,4,8,3]}

Start with nothing on the left (left = 0) and everything on the right (right = 25).

nums: left side | index i | right side

10

0

4

1

8

2

3

3

running sums (scale 0 to total 25)

left

0

right

25

gap

-

answer (0/4)

Variables
VariableValue
left0
right25
DepthFunction Call
Stack empty
0/18