Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Advanced & Niche

Meet in the Middle

O(2^(n/2))

Enumerate each half (2^(n/2) each), sort one, and binary-search to combine.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn firstSubsets / Permutations / CombinationsBinary SearchHash Map / Hash Set Lookup
0%
mastery
0/5 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

n around 35-45: 2ⁿ is too big but 2^(n/2) fits; subset/closest sum with huge values; ~20x20 grid path with XOR.

  1. 1Brute force

    Enumerate all 2n subsets.

  2. 2The observation

    2n enumeration: each full solution is a pair of half-solutions, matched in sorted order.

  3. 3Why it works

    Every full subset corresponds to exactly one (left half, right half) pair.

Reach for it when

  • +subset sum / closest sum with huge values
  • +path in grid of size ~20x20 with XOR

Not when

  • ×Values are small enough for a sum-indexed tableuse Knapsack DP
  • ×The combine step needs full state, not a pair of half-resultsuse No clean split; meet in the middle does not apply

Practice

Next · Foundation

Apple Division

Enumerate 2^n subsets; MITM is needed only when n doubles.

0/5 solved
  • Apple DivisionFoundationFoundationCSESEasy
  • Meet in the MiddleApply it · Count subsets with sum x, n = 40.Apply itCSESMedium
  • Closest Subsequence SumOne change · Closest instead of exact.One changeHard
  • Partition Array Into Two Arrays to Minimize Sum DifferenceTwists · Exactly n elements per side.TwistsHard
  • Xor-PathsMastery checkMastery checkCodeforces2100

Pitfalls

Classic bugs

  • !Subset size constrained (exactly k items): group each half's sums by size and combine size i with size k-i.
Go deeper1 combinations · resources

Combinations

  • Meet in the Middle+Binary Search

    n ~ 40 subset sum (exact, closest, count)

    Meet in the Middle:
    enumerates half-subsets
    Binary Search:
    matches each left sum to the best right sum

    Closest Subsequence SumPartition Array Into Two Arrays to Minimize Sum DifferenceMeet in the Middle

Resources

  • Segment Treecp-algorithms · article
  • Fenwick Treecp-algorithms · article
  • ITMO Segment Tree courseCodeforces EDU · course
  • Efficient and easy segment treesAl.cash · blog
  • Meet in the MiddleUSACO Guide · course
  • Matrix ExponentiationUSACO Guide · course
  • Reservoir samplingWikipedia · article
Reservoir SamplingMatrix Exponentiation
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial