Computer Science and IT‹›⌂ 0:00 studied

Module 6 · 15 min read · Free chapter

Divide and conquer

In short

Divide and conquer splits a problem into smaller copies, solves them recursively and combines the answers. Its cost follows a recurrence such as T(n) = 2T(n/2) + n, which gives Θ(n log n). Strassen uses 7 products instead of 8, giving about Θ(n^2.81), and Karatsuba uses 3 instead of 4.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: counting votes in a big constituency

Think of counting day in a constituency with 1,000 polling booths. No single officer counts every vote alone. The boxes are split among many tables, each table counts its own share, and the table totals are added to give the result.

That is divide and conquer. You break a big problem into smaller problems of the same kind, solve each smaller problem in the same way, and then combine the small answers into the big answer. The smaller problems can themselves be split again, until each piece is so small that the answer is obvious.

The trick only pays off if splitting and combining are cheap. Adding table totals is quick, so the method works well for counting. In this chapter we will see problems where a clever combine step turns a slow method into a fast one.

Why it matters. Merge sort, quicksort, binary search, fast multiplication and many graphics and geometry methods all follow this one pattern. GATE asks you to write their recurrences, solve them and compare them, so the pattern is worth learning once and well.

The pattern and its recurrence

Every divide-and-conquer algorithm has three steps. Divide the input into aa smaller subproblems, each of size about nb\dfrac{n}{b}. Conquer each subproblem by a recursive call, and stop the recursion at a small base case. Combine the answers of the subproblems into the answer for the whole input.

If dividing and combining together cost f(n)f(n), the running time obeys T(n)=aT(nb)+f(n)T(n) = aT(\dfrac{n}{b}) + f(n), with T of a small constant equal to a constant. This is exactly the form that the master theorem and the recursion tree solve, so the analysis of every algorithm in this chapter starts by reading off a, b and f(n)f(n).

Binary search is the simplest case. It divides the sorted array at the middle, keeps only one half and throws the other away, and has nothing to combine. So a=1a = 1, b=2b = 2 and f(n)=cf(n) = c, which gives T(n)=T(n2)+c=Θ(log⁡n)T(n) = T(\dfrac{n}{2}) + c = \Theta(\log n).

Fast power uses the same shape. To compute x16x^{16} the plain way you need 15 multiplications, but x16=(x8)2x^{16} = (x^8)^2, x8=(x4)2x^8 = (x^4)^2, x4=(x2)2x^4 = (x^2)^2 and x2=x⋅xx^2 = x \cdot x, which is only 4 multiplications. For odd nn, xn=x⋅(xn−1)x^n = x \cdot (x^{n - 1}), so in general the number of multiplications is Θ(log⁡n)\Theta(\log n).

power(x, n):
    if n == 0: return 1
    h = power(x, n / 2)          // integer division
    if n is even: return h * h
    else: return x * h * h

Computers use this idea to raise numbers to huge powers in encryption. Some people call these one-subproblem cases "decrease and conquer", because nothing needs combining.

Why it matters. Once you can read a, b and f(n)f(n) from the code, the answer usually follows in one line. The rest of this chapter shows how changing a or f(n)f(n), even by a little, changes the final running time.

Counting comparisons exactly: max-min and merge sort

GATE often asks not just for Θ\Theta but for the exact number of comparisons. Two classic examples show how to get such counts from a recurrence with its base case.

Finding the maximum and the minimum together. The plain way scans once for the maximum, using n−1n - 1 comparisons, and once for the minimum, using another n−1n - 1, so 2(n−1)2(n - 1) in total. Divide and conquer does better. Split the array into halves, find the max and min of each half, then compare the two maxima and the two minima, which costs 2 more comparisons.

This gives T(n)=2T(n2)+2T(n) = 2T(\dfrac{n}{2}) + 2, with T(2)=1T(2) = 1 because one comparison of two elements decides both, and T(1)=0T(1) = 0. Unrolling for n=8n = 8 gives T(2)=1T(2) = 1, T(4)=2⋅1+2=4T(4) = 2 \cdot 1 + 2 = 4 and T(8)=2⋅4+2=10T(8) = 2 \cdot 4 + 2 = 10. In general, for nn a power of 2, T(n)=3n2−2T(n) = \dfrac{3n}{2} - 2, against 2n−2=142n - 2 = 14 for the plain way.

The same count comes from a simple pairing view. Compare the elements in n2\dfrac{n}{2} pairs, then find the maximum among the n2\dfrac{n}{2} winners and the minimum among the n2\dfrac{n}{2} losers. That costs n2+(n2−1)+(n2−1)=3n2−2\dfrac{n}{2} + (\dfrac{n}{2} - 1) + (\dfrac{n}{2} - 1) = \dfrac{3n}{2} - 2, and for general nn the count is ⌈3n2⌉−2\lceil \dfrac{3n}{2} \rceil - 2, which no comparison method can beat.

Merge sort. Its recurrence is T(n)=2T(n2)+Θ(n)T(n) = 2T(\dfrac{n}{2}) + \Theta(n), which gives Θ(nlog⁡n)\Theta(n \log n). For the exact worst case, merging two lists of size n2\dfrac{n}{2} needs at most n−1n - 1 comparisons, so C(n)=2C(n2)+n−1C(n) = 2C(\dfrac{n}{2}) + n - 1 with C(1)=0C(1) = 0. For n=8n = 8 this is C(2)=1C(2) = 1, C(4)=2⋅1+3=5C(4) = 2 \cdot 1 + 3 = 5 and C(8)=2⋅5+7=17C(8) = 2 \cdot 5 + 7 = 17, which matches the formula nlog⁡2n−n+1=24−8+1n \log_2 n - n + 1 = 24 - 8 + 1.

Why it matters. Exact counts separate a well-prepared candidate from a guesser, because the options in such questions are often 10, 12, 14 and 16. Writing the base case correctly is half the work.

Quicksort analysed: worst, best, skewed and random pivots

Quicksort does its work in the divide step: partitioning nn elements around a pivot takes n−1n - 1 comparisons, and then the two parts are sorted separately. There is nothing to combine. So its running time depends entirely on how evenly the pivot splits the array.

Worst case. If the pivot is always the smallest or largest element, one part is empty and the other has n−1n - 1 elements. Then T(n)=T(n−1)+(n−1)T(n) = T(n - 1) + (n - 1), which adds up to n(n−1)2\dfrac{n(n - 1)}{2} comparisons, that is Θ(n2)\Theta(n^2). With the first or last element as pivot, this happens on sorted and reverse-sorted input.

Best case. If the pivot is always the median, T(n)=2T(n2)+Θ(n)T(n) = 2T(\dfrac{n}{2}) + \Theta(n), the same as merge sort, so T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n).

A skewed but fixed split is still fine. Suppose every pivot splits the array 1 : 9. Then T(n)=T(n10)+T(9n10)+cnT(n) = T(\dfrac{n}{10}) + T(\dfrac{9n}{10}) + cn. In the recursion tree every full level costs at most cncn, and the deepest path has length log⁡10/9n\log_{10/9} n, which is still Θ(log⁡n)\Theta(\log n), so T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n).

So quicksort is slow only when the splits are lopsided again and again. Even a split of 1 : 99 at every step gives Θ(nlog⁡n)\Theta(n \log n); only a split that leaves a constant number of elements on one side, again and again, gives Θ(n2)\Theta(n^2).

Randomised quicksort chooses the pivot uniformly at random from the current part. Now no particular input is bad, because the luck lies in the coin tosses and not in the data.

Sort the elements by rank as z1<z2<⋯<znz_1 < z_2 < \dots < z_n. The elements ziz_i and zjz_j are compared only if one of them is the first pivot chosen from ziz_i, ..., zjz_j, which has probability 2j−i+1\dfrac{2}{j - i + 1}.

Adding these probabilities over all pairs gives an expected number of comparisons of about 2nln⁡n2n \ln n, roughly 1.39 nlog⁡2nn \log_2 n, for every input. So the expected time is Θ(nlog⁡n)\Theta(n \log n). The worst case is still Θ(n2)\Theta(n^2), but it needs extremely bad luck.

Why it matters. This is why library sorts use random or "median of three" pivots. The analysis also teaches a key idea: an algorithm can be made safe against any input by putting the randomness inside the algorithm.

Fewer multiplications: Karatsuba and Strassen

Sometimes divide and conquer wins not by splitting but by saving one recursive call. The two best-known examples are multiplying large numbers and multiplying matrices.

Karatsuba multiplication. The school method multiplies two nn-digit numbers in Θ(n2)\Theta(n^2) digit operations. Split each number into halves of m=n2m = \dfrac{n}{2} digits: x=a⋅10m+bx = a \cdot 10^m + b and y=c⋅10m+dy = c \cdot 10^m + d. Then xy=ac⋅102m+(ad+bc)⋅10m+bdxy = ac \cdot 10^{2m} + (ad + bc) \cdot 10^m + bd, which seems to need four half-size products.

The key observation is that (a+b)(c+d)=ac+ad+bc+bd(a + b)(c + d) = ac + ad + bc + bd. So the middle term is ad+bc=(a+b)(c+d)−ac−bdad + bc = (a + b)(c + d) - ac - bd, and three products are enough. The recurrence becomes T(n)=3T(n2)+Θ(n)T(n) = 3T(\dfrac{n}{2}) + \Theta(n), and the master theorem gives Θ(nlog⁡23)\Theta(n^{\log_2 3}), which is about n1.59n^{1.59}.

Worked example. Multiply 1234×56781234 \times 5678 with a=12a = 12, b=34b = 34, c=56c = 56 and d=78d = 78. The three products are ac=672ac = 672, bd=2652bd = 2652 and (a+b)(c+d)=46×134=6164(a + b)(c + d) = 46 \times 134 = 6164. The middle term is 6164−672−2652=28406164 - 672 - 2652 = 2840, so the answer is 672×104+2840×102+2652=7,006,652672 \times 10^4 + 2840 \times 10^2 + 2652 = 7{,}006{,}652.

Matrix multiplication. The direct method for n×nn \times n matrices uses n3n^3 scalar multiplications. Splitting each matrix into four n2×n2\dfrac{n}{2} \times \dfrac{n}{2} blocks gives eight block products, so T(n)=8T(n2)+Θ(n2)=Θ(n3)T(n) = 8T(\dfrac{n}{2}) + \Theta(n^2) = \Theta(n^3), and splitting alone gains nothing.

Strassen's method forms seven special block products using sums and differences of blocks, and then rebuilds the four result blocks from them. It uses 7 block products and 18 block additions or subtractions. So T(n)=7T(n2)+Θ(n2)=Θ(nlog⁡27)T(n) = 7T(\dfrac{n}{2}) + \Theta(n^2) = \Theta(n^{\log_2 7}), which is about n2.81n^{2.81}.

In practice the extra additions make Strassen's method pay off only for fairly large matrices, and it can be less accurate with floating-point numbers. Karatsuba likewise beats the school method only beyond some hundreds of digits.

Why it matters. These two methods show that the exponent depends on a, the number of subproblems. Cutting a from 4 to 3 or from 8 to 7 changes the growth rate itself, which no faster processor can match for large inputs.

Smart combine steps: counting inversions and the closest pair

In some problems the brute-force method checks all n(n−1)2\dfrac{n(n - 1)}{2} pairs. Divide and conquer cuts this to nlog⁡nn \log n when the combine step can deal with all the "cross" pairs, with one element in each half, in linear time.

Counting inversions. An inversion is a pair i<ji < j with a[i]>a[j]a[i] > a[j]. It measures how far an array is from sorted, which is how music and shopping sites compare two people's rankings. Checking every pair takes Θ(n2)\Theta(n^2).

Instead, run merge sort and count while merging. Inversions inside the left half and inside the right half are counted by the recursive calls. When the merge takes an element from the right half, that element is smaller than every element still waiting in the left half, so we add the number of waiting left elements.

Worked example. Take 2, 4, 1, 3, 5, split as 2, 4 and 1, 3, 5, each half already sorted with 0 inversions. The merge takes 1 from the right while 2 and 4 wait, adding 2, then takes 2, then takes 3 while 4 waits, adding 1. The total is 3, which are the pairs (2, 1), (4, 1) and (4, 3), and the time is T(n)=2T(n2)+Θ(n)=Θ(nlog⁡n)T(n) = 2T(\dfrac{n}{2}) + \Theta(n) = \Theta(n \log n).

Closest pair of points. Given nn points in a plane, find the two that are nearest to each other. Sort the points by xx and split them by a vertical line through the median xx. Solve each half recursively and let δ\delta be the smaller of the two best distances.

The only pairs left to check have one point on each side of the line, and both such points must lie within δ\delta of the line. Keep only the points in this strip of width 2δ, sorted by yy. A packing argument shows that each strip point needs to be compared only with a constant number of the points that follow it, so the combine step is linear.

With the points sorted by xx and yy once at the start, T(n)=2T(n2)+O(n)=O(nlog⁡n)T(n) = 2T(\dfrac{n}{2}) + O(n) = O(n \log n). If the strip is sorted afresh at every level, the time becomes O(nlog⁡2n)O(n \log^2 n), which is still far better than the Θ(n2)\Theta(n^2) of checking every pair.

Why it matters. These two problems show the real art of divide and conquer, which is designing a combine step that handles all cross pairs without looking at them one by one. Air traffic systems use closest-pair ideas, and ranking comparison is a direct use of inversion counting.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university tests ask about divide and conquer in four ways:

  1. Explain the paradigm with its three steps and a worked example, usually merge sort or binary search.
  2. Derive the recurrence of a given algorithm and solve it by the recursion tree or the master theorem.
  3. Analyse quicksort in its best, worst and average cases and explain the role of the pivot.
  4. Show how a clever split reduces work, as in Karatsuba or Strassen, and compute the new exponent.

Practice questions (our own, not previous-year questions).

  1. Write a divide-and-conquer algorithm that finds both the maximum and the minimum of nn numbers, and prove that it uses 3n2−2\dfrac{3n}{2} - 2 comparisons when nn is a power of 2. (10 marks)
  2. Explain why randomised quicksort has an expected running time of Θ(nlog⁡n)\Theta(n \log n) for every input. (10 marks)
  3. Describe Karatsuba's method, apply it to 2143×34122143 \times 3412 and derive its running time. (15 marks)

Answer plan for question 3.

  • Explain that the school method needs Θ(n2)\Theta(n^2) digit operations because every digit of one number meets every digit of the other.
  • Split x=a⋅10m+bx = a \cdot 10^m + b and y=c⋅10m+dy = c \cdot 10^m + d, and write xy=ac⋅102m+(ad+bc)⋅10m+bdxy = ac \cdot 10^{2m} + (ad + bc) \cdot 10^m + bd.
  • Show that ad+bc=(a+b)(c+d)−ac−bdad + bc = (a + b)(c + d) - ac - bd, so only three half-size products are needed.
  • Apply it with a=21a = 21, b=43b = 43, c=34c = 34 and d=12d = 12, giving ac=714ac = 714, bd=516bd = 516,(a+b)(c+d)=64×46=2944(a + b)(c + d) = 64 \times 46 = 2944 and a middle term of 1714.
  • Assemble 714×104+1714×102+516=7,311,916714 \times 10^4 + 1714 \times 10^2 + 516 = 7{,}311{,}916 and check it by direct multiplication.
  • Write T(n)=3T(n2)+Θ(n)T(n) = 3T(\dfrac{n}{2}) + \Theta(n), apply case 1 of the master theorem with p=log⁡23≈1.585p = \log_2 3 \approx 1.585, and conclude that T(n)=Θ(nlog⁡23)T(n) = \Theta(n^{\log_2 3}).

Quick revision

  • Divide and conquer divides a problem, solves the parts recursively and combines the answers, giving T(n)=aT(nb)+f(n)T(n) = aT(\dfrac{n}{b}) + f(n).
  • Binary search and fast power keep one half-size subproblem, so they take Θ(log⁡n)\Theta(\log n).
  • Max-min by divide and conquer takes 3n2−2\dfrac{3n}{2} - 2 comparisons for nn a power of 2, against 2n−22n - 2 for the plain way.
  • Merge sort takes nlog⁡2n−n+1n \log_2 n - n + 1 comparisons in the worst case for nn a power of 2, which is 17 for n=8n = 8.
  • Quicksort is Θ(n2)\Theta(n^2) when the pivot is always an extreme element and Θ(nlog⁡n)\Theta(n \log n) for any fixed-proportion split.
  • Randomised quicksort makes about 1.39 nlog⁡2nn \log_2 n comparisons on average for every input.
  • Karatsuba uses 3 half-size products and runs in about n1.59n^{1.59} time.
  • Strassen uses 7 block products and runs in about n2.81n^{2.81} time, against n3n^3 for 8 products.
  • Inversions are counted inside merge sort in Θ(nlog⁡n)\Theta(n \log n).
  • The closest pair of points is found in O(nlog⁡n)O(n \log n) by checking a narrow strip around the dividing line.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 14 answered

  1. The three steps of divide and conquer, in order, are:

  2. An algorithm splits a problem of size nn into 4 subproblems of size n2\dfrac{n}{2} and spends Θ(n)\Theta(n) to combine. Its recurrence is:

  3. Computing x16x^{16} by repeated squaring needs how many multiplications?

  4. A power function computes `power(x, n/2)` twice instead of storing it once. Its number of multiplications grows as:

  5. The number of comparisons used by divide and conquer to find both the maximum and minimum of 16 numbers is:

  6. The worst-case number of comparisons made by merge sort on 8 elements is:

  7. If every partition in quicksort splits the array in the ratio 1 : 9, the running time is:

  8. The worst-case recurrence of quicksort is:

  9. In randomised quicksort, the probability that the elements of ranks ii and jj (i<j)(i < j) are ever compared is:

  10. Karatsuba multiplication of two nn-digit numbers runs in:

  11. Strassen's matrix multiplication uses how many multiplications of half-size blocks?

  12. Multiplying n×nn \times n matrices by splitting into four blocks and doing all 8 block products takes:

  13. The number of inversions in the array 3, 1, 4, 2 is:

  14. With points presorted by xx and yy, the divide-and-conquer closest-pair method runs in:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What are the three steps of divide and conquer?

Divide the problem into smaller subproblems, conquer each one recursively, and combine their answers into the answer for the whole.

What is the time complexity of Strassen's algorithm?

T(n) = 7T(n/2) + Θ(n²), which gives Θ(n^(log₂ 7)), about Θ(n^2.81).

How many comparisons find both the maximum and minimum?

Comparing elements in pairs needs about 3n/2 − 2 comparisons, fewer than the 2n − 2 of two separate scans.

How is xⁿ computed in Θ(log n) multiplications?

Square the answer for x^(n/2) and multiply by x once more when n is odd, so the exponent halves at every step.

More in this module: Algorithms

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Algorithms (IIT Kharagpur, 106105164): divide and conquer
  3. NPTEL Programming, Data Structures and Algorithms (CMI, 106106145): divide and conquer, counting inversions, closest pair