Left and Right Sum Differences
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|.
An integer array nums of length n.
An array of n integers: answer[i] is the absolute difference between the sum before i and the sum after i.
Left sums are [0, 10, 14, 22] and right sums are [15, 11, 3, 0]. Their differences are [15, 1, 11, 22].
Both sides of the only index are empty, so |0 − 0| = 0.
- • 1 ≤ n ≤ 1000
- • 1 ≤ nums[i] ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each index, loop over the left side and the right side
Build all left sums in one pass and all right sums in another, then subtract
One running left sum; right = total − left − nums[i]
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.