Partition Into 2 Subsets with Min Sum Diff
Problem Statement
You get a list of positive whole numbers, arr. Split the list into two groups so that every number is in exactly one group. Make the difference between the two group sums as small as possible.
Return that smallest possible difference.
An array of positive integers arr.
One number: the smallest possible difference between the sums of the two groups. The function is int minDifference(vector<int>& arr).
The total is 23. The groups [11] and [1, 6, 5] sum to 11 and 12, so the difference is 1. A difference of 0 is impossible, because 23 is odd.
The total is 10. The groups [7] and [1, 2] sum to 7 and 3, so the difference is 4. No other split does better.
The total is 12. The groups [3, 3] and [2, 2, 2] both sum to 6, so the difference is 0.
- • 1 ≤ arr.size() · (sum of the array) ≤ 10^5
- • 1 ≤ arr[i] ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try every subset as group A; keep the largest sum that is not over half
dp[i][s] = can the first i numbers make sum s (s up to total / 2)? The last row holds the answers
One true / false row over sums 0 … total / 2; the first true cell from the top is the answer
Call one group A and the other group B. Try every subset as A. If A's sum is s, B's sum is total − s. Only the sums up to total / 2 matter, and the largest one gives the smallest difference, which is total − 2s. There are 2^N subsets, so the search checks all of them.
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 — Knapsack 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.