2Brute2Best
🔥0

Left and Right Sum Differences

Easy·LeetCode #2574 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given an integer array nums of length n.

For each index i, let left be the sum of the numbers before i, and right be the sum of the numbers after i. nums[i] itself is in neither. An empty side has sum 0.

Return an array answer of length n where answer[i] = |left − right|.

Input

An integer array nums of length n.

Output

An array of n integers: answer[i] is the absolute difference between the sum before i and the sum after i.

Example 1
Input: nums = [10, 4, 8, 3]
Output: [15, 1, 11, 22]

Left sums are [0, 10, 14, 22] and right sums are [15, 11, 3, 0]. Their differences are [15, 1, 11, 22].

Example 2
Input: nums = [1]
Output: [0]

Both sides of the only index are empty, so |0 − 0| = 0.

Constraints
  • • 1 ≤ n ≤ 1000
  • • 1 ≤ nums[i] ≤ 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“sum of the numbers before i”
→ Prefix sum ending just before i — a running total from the left.
“sum of the numbers after i”
→ Suffix sum starting just after i — or total minus the rest.
“for each index”
→ Every index needs both sides → left pass + right pass (or total + running left).
03

Approach Complexity Ladder

Brute Force — sum both sides for every indexO(N²)O(1)

For each index, loop over the left side and the right side

Better — left-sum array + right-sum arrayO(N)O(N)

Build all left sums in one pass and all right sums in another, then subtract

Optimal — total minus running leftO(N)O(1)

One running left sum; right = total − left − nums[i]

Brute Force Intuition

For each i, add up nums[0 … i−1] and nums[i+1 … n−1] with two loops and store the absolute difference.

Recognition rule for this sub-pattern:

Each index needs information from its left and its right. Build a left pass and a right pass, then combine.
Remember this · Prefix Sum
Trigger
Range sums, "subarray sum equals K" (especially with negatives), equal counts, divisible by K.
Think
If I knew the total up to every index, would the answer become a difference of two totals?
Key question
Which earlier prefix value am I looking for when I stand at index i?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.