#2574

Left and Right Sum Differences

Easy Prefix & suffix

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

For every index, return the absolute difference between the sum of elements to its left and the sum of elements to its right.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: for each index, |sum on the left - sum on the right|.

Why this works: same trick as the pivot index: right = total - left - nums[i].

Example: [10, 4, 8, 3], total 25

i=0: left 0,  right 15 → 15i=1: left 10, right 11 → 1i=2: left 14, right 3  → 11i=3: left 22, right 0  → 22

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Including nums[i] in either side.