2Brute2Best
🔥0

Partition Into 2 Subsets with Min Sum Diff

Medium·GeeksforGeeks Pro OnlySlight Variation
GeeksforGeeks
01

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.

Input

An array of positive integers arr.

Output

One number: the smallest possible difference between the sums of the two groups. The function is int minDifference(vector<int>& arr).

Example 1
Input: arr = [1, 6, 11, 5]
Output: 1

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.

Example 2
Input: arr = [1, 2, 7]
Output: 4

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.

Example 3
Input: arr = [3, 3, 2, 2, 2]
Output: 0

The total is 12. The groups [3, 3] and [2, 2, 2] both sum to 6, so the difference is 0.

Constraints
  • • 1 ≤ arr.size() · (sum of the array) ≤ 10^5
  • • 1 ≤ arr[i] ≤ 10^5
02

Pattern Recognition Clues

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

“split into two subsets”
→ Each number goes into one of two groups: the same 0/1 choice as Partition Equal Subset Sum.
“minimum difference between the two sums”
→ The difference is total − 2s, so we want the largest group sum s that is not over total / 2.
“return the difference, not the groups”
→ We only need the best sum, so one bool row and one scan are enough.
03

Approach Complexity Ladder

Brute Force — every subsetO(2^N)O(N)

Try every subset as group A; keep the largest sum that is not over half

Better — 2D table of reachable sumsO(N · S)O(N · S)

dp[i][s] = can the first i numbers make sum s (s up to total / 2)? The last row holds the answers

Optimal — one row, scan down from halfO(N · S)O(S)

One true / false row over sums 0 … total / 2; the first true cell from the top is the answer

Brute Force Intuition

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:

Two equal/close groups ⇔ find a subset with sum near total/2.
Remember this · DP — Knapsack
Trigger
Pick a subset to hit a target sum / capacity, partition into two groups, count the ways to reach an amount.
Think
Is this "choose some items so their total is X"?
Key question
Can each item be used once (loop capacity backwards) or many times (loop forwards)?
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 DP — Knapsack translate into production architectures and low-level memory mechanics.