1. Gyanastra
  2. Algorithms
  3. Divide and Conquer: Max-Min, Maximum Subarray, Inversions and Fast Exponentiation
  4. The Divide-and-Conquer Template
Free lesson, no account needed 6 min read
Asked in past papers2 times
  • GATE CSE20142007

Algorithms · Divide and Conquer: Max-Min, Maximum Subarray, Inversions and Fast Exponentiation

The Divide-and-Conquer Template

Every divide-and-conquer (D&C) algorithm has three phases:

  1. Divide the problem of size nn into aa subproblems, each of size n/bn/b.
  2. Conquer each subproblem recursively (or directly, if small enough — the base case).
  3. Combine the aa sub-solutions into one solution, at cost f(n)f(n).

The running time therefore satisfies

T(n)=a T(n/b)+f(n),T(1)=Θ(1)T(n) = a\,T(n/b) + f(n), \qquad T(1) = \Theta(1)

where aa = number of subproblems, bb = shrink factor, f(n)f(n) = divide + combine cost. This is solved by the Master Theorem (previous chapter). For the algorithms below, the recurrences are:

AlgorithmRecurrenceSolution
Max-MinT(n)=2T(n/2)+2T(n)=2T(n/2)+23n2−2\tfrac{3n}{2}-2 comparisons
Merge sort / inversion countT(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)Θ(nlog⁡n)\Theta(n\log n)
Maximum subarray (D&C)T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)Θ(nlog⁡n)\Theta(n\log n)
Binary searchT(n)=T(n/2)+O(1)T(n)=T(n/2)+O(1)O(log⁡n)O(\log n)
Fast exponentiationT(n)=T(n/2)+O(1)T(n)=T(n/2)+O(1)O(log⁡n)O(\log n) multiplications
Tower of HanoiT(n)=2T(n−1)+1T(n)=2T(n-1)+12n−12^n-1 moves

Note the last one: Hanoi shrinks by a constant (n−1n-1), not by a factor, so the Master Theorem does not apply — expand the recurrence directly.


2.1 The naive method and its cost

Find max with n−1n-1 comparisons, then min with n−1n-1 comparisons: total 2n−22n-2.

2.2 The D&C method

Divide the array into two halves. Conquer to get (max, min) of each half. Combine with exactly 2 comparisons: compare the two maxima, compare the two minima.

T(n)=2T(n/2)+2,T(2)=1T(n) = 2T(n/2) + 2, \qquad T(2) = 1

(The base case of two elements needs 1 comparison.)

Solving the recurrence by expansion. Let n=2kn = 2^k and stop at the two-element base case T(2)=1T(2)=1. (The base must be T(2)=1T(2)=1; with T(1)=0T(1)=0 the same recurrence would give the naive 2n−22n-2.) Unrolling k−1k-1 times:

T(n)=2k−1T(2)+(2+4+⋯+2k−1)=n2+(n−2)=3n2−2.T(n) = 2^{k-1}T(2) + (2 + 4 + \dots + 2^{k-1}) = \frac{n}{2} + (n-2) = \frac{3n}{2} - 2.

The same count is obtained without recursion by the pairwise (tournament) method, which is the standard "3n/2 − 2" result:

Pairwise method. Pair up the nn elements: ⌊n/2⌋\lfloor n/2 \rfloor pairs, each costing 1 comparison (winner = candidate max, loser = candidate min). Then scan the ⌊n/2⌋\lfloor n/2\rfloor candidates-max for the maximum (⌊n/2⌋−1\lfloor n/2\rfloor - 1 comparisons) and the ⌊n/2⌋\lfloor n/2\rfloor candidates-min for the minimum (⌊n/2⌋−1\lfloor n/2\rfloor - 1 comparisons).

Total for even nn:

n2+(n2−1)+(n2−1)=3n2−2\frac{n}{2} + \left(\frac{n}{2}-1\right) + \left(\frac{n}{2}-1\right) = \frac{3n}{2} - 2

For odd nn: ⌈3n/2⌉−2\lceil 3n/2\rceil - 2 comparisons.

Worked check, n=8n = 8. Pairs: 4 comparisons. Candidates-max: 4 elements → 3 comparisons. Candidates-min: 4 elements → 3 comparisons. Total =4+3+3=10= 4+3+3 = 10. And 3⋅82−2=12−2=10\tfrac{3\cdot 8}{2}-2 = 12-2 = 10. ✓

Worked check, n=6n = 6. Pairs: 3 comparisons. Candidates-max: 3 → 2 comparisons. Candidates-min: 3 → 2 comparisons. Total =3+2+2=7= 3+2+2 = 7. Formula: 3⋅62−2=9−2=7\tfrac{3\cdot 6}{2}-2 = 9-2 = 7. ✓

Worked check, n=5n = 5 (odd). Two pairs (2 comparisons) + one leftover element. Candidates-max: 2 winners + 1 leftover = 3 elements → 2 comparisons. Candidates-min: 2 losers + 1 leftover = 3 elements → 2 comparisons. Total =2+2+2=6= 2+2+2 = 6. Formula: ⌈15/2⌉−2=8−2=6\lceil 15/2\rceil - 2 = 8-2 = 6. ✓

2.3 Why 3n/2 − 2 is optimal

Each element must "lose" at least once to be excluded from being max, and "win" at least once to be excluded from being min. Elements that lose a comparison can never be max; elements that win can never be min. A comparison between two elements that have both already won (or both already lost) wastes information. The pairwise scheme makes every comparison informative, achieving the lower bound ⌈3n/2⌉−2\lceil 3n/2\rceil - 2.

2.4 The D&C recurrence, stated correctly

The clean D&C recurrence that yields the same count is

T(n)=2T(n/2)+2,T(2)=1T(n) = 2T(n/2) + 2,\quad T(2)=1

For n=8n=8: T(8)=2T(4)+2=2(2T(2)+2)+2=2(2⋅1+2)+2=2⋅4+2=10T(8) = 2T(4)+2 = 2(2T(2)+2)+2 = 2(2\cdot1+2)+2 = 2\cdot4+2 = 10. ✓ Matches the pairwise count. For n=4n=4: T(4)=2T(2)+2=2+2=4T(4)=2T(2)+2 = 2+2 = 4, and 3⋅42−2=4\tfrac{3\cdot4}{2}-2 = 4. ✓


Binary Search Variants

All are D&C with one recursive call: T(n)=T(n/2)+O(1)=O(log⁡n)T(n)=T(n/2)+O(1) = O(\log n). The array must be sorted (except in the rotated case, where it is sorted-then-rotated).

3.2 First occurrence (lower bound)

When A[mid] == key, do not return; record ans = mid and continue left (hi = mid-1). This finds the leftmost index. Still O(log⁡n)O(\log n).

3.3 Last occurrence (upper bound)

Symmetric: on equality record and go right (lo = mid+1).

3.4 Rotated sorted array

Array sorted then rotated, e.g. [4,5,6,7,0,1,2]. At each step, one half is sorted. Check which:

  • If A[lo] <= A[mid], the left half is sorted. If A[lo] <= key < A[mid], search left; else search right.
  • Otherwise the right half is sorted. If A[mid] < key <= A[hi], search right; else search left.

O(log⁡n)O(\log n). With duplicates the worst case degrades to O(n)O(n) (e.g. [1,1,1,0,1]).

3.5 Peak element

A peak is an index ii with A[i-1] <= A[i] >= A[i+1] (boundaries count one side). If A[mid] < A[mid+1], a peak lies to the right (lo = mid+1); else to the left (hi = mid). O(log⁡n)O(\log n), works on unsorted arrays.

VariantPreconditionTime
ClassicsortedO(log⁡n)O(\log n)
First / last occurrencesortedO(log⁡n)O(\log n)
Rotated (distinct)rotated sortedO(log⁡n)O(\log n)
Rotated (duplicates)rotated sortedO(n)O(n) worst
PeakanyO(log⁡n)O(\log n)

Pages 1–2 of 2