Maximum Sum of 3 Non-Overlapping Subarrays
Problem Statement
You are given an integer array nums and an integer k. Pick three subarrays of length k that do not overlap: no index may belong to two of them. Choose them so that the total of their sums is as large as possible.
Return the starting indices of the three subarrays, in increasing order (0-indexed). If several choices give the same total, return the lexicographically smallest answer: compare the first index, then the second, then the third.
An integer array nums and an integer k.
Three starting indices [a, b, c] of non-overlapping length-k subarrays with the largest total sum, lexicographically smallest on a tie.
The windows [1, 2], [2, 6] and [7, 5] start at 0, 3 and 5. Their sums are 3, 8 and 12, a total of 23. [1, 3, 5] has the same total, but [0, 3, 5] is smaller.
Every window of length 2 sums to 3, so any three windows that do not overlap give the same total. [0, 2, 4] is the smallest choice.
- • 1 ≤ nums.length ≤ 2 × 10^4
- • 1 ≤ nums[i] < 2^16
- • 1 ≤ k ≤ floor(nums.length / 3)
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Check every valid triple a < b < c and keep the best total
For each middle window, scan the left and the right for their best start
Build left[] and right[] in one pass each, then try each middle with two lookups
Compute every window sum W[s] first. Then try every triple of starts where each pair is at least k apart. Keep the best total. A tie keeps the earlier triple, which is the lexicographically smaller one.
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.