Minimum Sum Subarray
Problem Statement
You are given an integer array a of length n. Find a contiguous part of it, a subarray with at least one number, whose sum is as small as possible. Return that smallest sum.
A subarray is a run of neighbouring numbers. It must hold at least one number, so an empty run (sum 0) is not allowed.
GFG's function is int minSubarraySum(vector<int>& arr). The code here returns long long: with up to 10^6 numbers of size 10^7, a sum can reach 10^13, which doesn't fit in an int.
An integer array a of length n.
One number: the smallest sum of any non-empty subarray.
The run [-4, 2, -3, -1] adds up to -6. No other run of neighbours goes lower.
Every number is positive, so the smallest run is the single smallest number, 1. Adding more numbers only makes the sum bigger.
The whole array is the smallest run: -1 - 2 - 3 = -6.
- • 1 ≤ n ≤ 10^6
- • −10^7 ≤ a[i] ≤ 10^7
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each start, add one number at a time and keep the smallest sum
Track the smallest sum that ends at each index: extend it or restart
Try every start l. From each start, add the numbers one at a time, so each new sum is the old sum plus one number, and remember the smallest total.
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 DP — Kadane / Subarray 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.