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/Backtracking & Recursion/Backtracking

Technique

Divide and Conquer (merge-sort counting)

Split the array in half, solve each half, then count/merge cross pairs in linear time while merging sorted halves.

Learn firstSubsets / Permutations / CombinationsTwo PointersUnlocksCentroid DecompositionDivide & Conquer DP Optimisation
0%
mastery
0/10 solved
Practise nextSort an Array
IdeaPracticePitfalls

Recognise it

The signal

count inversions / pairs i<j with a condition on values

  1. 1Brute force

    Check all pairs O(n2).

  2. 2The observation

    Cross pairs: when both halves are sorted, cross pairs are counted with two pointers.

  3. 3Why it works

    Each pair (i, j) is counted exactly at the level where i and j are first split apart.

Reach for it when

  • +sort a linked list
  • +closest pair of points

Not when

  • ×online updates (use BIT)
  • ×condition not monotone on sorted halves

Practice

Next

Sort an Array

0/10 solved
  • Masha and a Beautiful TreeCodeforces1300
  • Permutation TransformationCodeforces1200
  • Sort an ArrayAmazon, MicrosoftMedium
  • Sort ListAmazon, AdobeMedium
  • a-Good StringCodeforces1500
  • Minimum Euclidean DistanceCSESHard
  • Count of Smaller Numbers After SelfGoogle, CitadelHard
  • Reverse PairsGoogle, CitadelHard
  • Count of Range SumHard
  • Merge k Sorted ListsAmazon, MetaHard

Pitfalls

Misconceptions

  • !counting during merge with the wrong pointer (reverse-pairs needs a separate counting pass)

Looks like it, isn't

  • Global and Local Inversions needs Invariants & Parity Arguments
    Why
    For a permutation of 0..n-1, global == local iff |nums[i] - i| <= 1 for all i. Counting inversions is unnecessary. source ↗
Go deepervariants and techniques · 1 combinations · resources

Variants and techniques

  • CDQ Divide and ConquerCP 2400+Split operations by time (or by one coordinate) and recurse on both halves. At each split, count how the left half's updates affect the right half's queries with a sweep plus a BIT. This removes one dimension at the cost of an extra log factor.

Combinations

  • CDQ Divide and Conquer+Segment Tree / Fenwick (BIT)

    2D point updates/queries (or swaps in permutations) all offline, n ~ 2e5

    CDQ Divide and Conquer:
    split operations by time; left updates feed right queries
    Segment Tree / Fenwick (BIT):
    a BIT over one coordinate during the merge sweep

    Intersection of PermutationsLittle Artem and Time Machine

Resources

  • Complete Search with RecursionUSACO Guide · course
  • Recursion I explore cardLeetCode · course
  • Recursion cheatsheetTech Interview Handbook · article
  • General approach to backtracking questionsLeetCode discuss · article
  • Competitive Programmer's HandbookAntti Laaksonen · book
TodayReviewPatternsProblems