Skip to the workspace
Pebellslearn by building
Analysis II: Recurrences, Amortisation & Randomness  ›  Lesson

Divide & Conquer Beyond Sorting

Divide and Conquer 32 minSplit a problem, solve the pieces recursively, and win by making the combine cheap or the calls few

dsa / Divide & Conquer Beyond Sorting

Explore each scene at your own pace.

01 · Fewer calls: multiplying big numbers

Fewer calls: multiplying big numbers

Divide & Conquer Beyond Sorting

Definition
Divide and conquer solves a problem by splitting the input into smaller parts, solving each part recursively, and combining the partial answers, and it pays off when the combine step is cheap or needs fewer recursive calls than the naive split.
In plain words
Think of sorting out a huge pile of paperwork by splitting it among helpers, each doing their share, then merging their results. It works well only when putting the pieces back together costs less than doing the whole job yourself.

In short: divide and conquer isn't only for sorting. Split the input, solve each half recursively, then combine the two answers — and the combine step is where the whole technique either pays off or doesn't. It wins when the combine is cheap (two half-size streak searches plus one linear crossing sweep) or when a clever combine needs fewer recursive calls than the obvious split (Karatsuba's three multiplies instead of four). Write the recurrence before you write the code: if the combine costs as much as solving the problem directly, or the halves overlap, divide and conquer won't help.

An 8-day array split at the middle: the best streak can live entirely left, entirely right, or cross the middle as a left tail plus a right head, 3 + 8 = 11

Merge sort taught you the pattern: divide the input, conquer each piece recursively, combine the answers. Lesson 1 gave you the tool to price it: T(n)=a T(n/b)+f(n)T(n) = a\,T(n/b) + f(n). This lesson uses the pattern on problems that have nothing to do with putting things in order, and uses the recurrence to predict, before you write a line, whether splitting will pay.

The rule of thumb from the master theorem: divide and conquer wins when the combine is cheaper than solving the whole problem directly, or when you can get away with fewer recursive calls than the obvious split needs.

Fewer calls: multiplying big numbers

Tunebox's royalty ledger multiplies very large integers. The schoolbook method multiplies every digit of one number by every digit of the other: n² single-digit multiplications for two n-digit numbers.

Divide and conquer splits each number into a high half and a low half, x=xh⋅10m+xlx = x_h \cdot 10^m + x_l, and expands the product:

x⋅y=xhyh⋅102m+(xhyl+xlyh)⋅10m+xlylx \cdot y = x_h y_h \cdot 10^{2m} + (x_h y_l + x_l y_h) \cdot 10^m + x_l y_l

That is four half-size multiplications: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n, which is still Θ(n2)\Theta(n^2). No gain. The trick, found by Karatsuba, is that the middle term can be had from one multiplication, reusing the other two:

xhyl+xlyh=(xh+xl)(yh+yl)−xhyh−xlylx_h y_l + x_l y_h = (x_h + x_l)(y_h + y_l) - x_h y_h - x_l y_l

Step through it on two 8-digit numbers. The array is the digits of x then y:

watch it move · array

Three half-size multiplies instead of four. One saved multiply per level compounds into a smaller exponent.

Split both numbers at m = 4 digits. The high halves are 1234 and 8765. 16 cells.

1 / 7

The recurrence becomes T(n)=3T(n/2)+nT(n) = 3T(n/2) + n. Three calls on halves, the leaves dominate, and the cost is nlog⁡23≈n1.585n^{\log_2 3} \approx n^{1.585}. Count it:

python

At 64 digits it uses under a third of the schoolbook count. The sums xh+xlx_h + x_l can carry into an extra digit, which inflates the small cases, but look at the ratios: each doubling of the digits multiplies Karatsuba's count by about 3 and the schoolbook's by 4.

❓ Cross-question — "Does Python do this?" Yes. CPython's integer multiply switches to Karatsuba once both numbers are several hundred digits long. Below that, the schoolbook loop's lower overhead wins. The numbers for that trade-off come next.

Karatsuba does more additions and bookkeeping per level than the schoolbook loop, so its real cost is roughly c⋅n1.585c \cdot n^{1.585} for some overhead c against n2n^2. At 1,000 digits that's c × 56,870 against 1,000,000:

overhead c Karatsuba schoolbook cheaper
10 568,706 1,000,000 Karatsuba
17 966,800 1,000,000 Karatsuba
18 1,023,670 1,000,000 schoolbook
30 1,706,117 1,000,000 schoolbook

Karatsuba wins only while c is below about 17.6. A better exponent always wins eventually, but the constant decides where "eventually" starts, which is why real libraries switch methods at a threshold.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — the cost lab
This piece — best_streak(): Finds a track's best run of days by splitting the history in half.
Scenario A label asks when their single peaked: which stretch of days gained the most listeners overall. Tunebox answers by splitting the history in half, over and over, and checking the stretch that crosses each middle.
best_streak.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
best_streak(deltas) → intThe largest total of any run of consecutive days.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.