About

Decrease by Factor & Variable-Size Decrease

Decrease-and-Conquer by a Factor and Variable-Size Decrease

Unlike decrease-by-a-constant, decrease-by-a-constant-factor reduces the problem instance size by a multiplicative factor (usually b=2b = 2, though sometimes b=3b = 3). Variable-size-decrease algorithms reduce instance size by an amount that varies dynamically from step to step.


1. Decrease-by-a-Constant-Factor

Binary Search (Factor of 2)

Given a sorted array A[0..n−1]A[0..n-1] and target key KK, compare KK with the middle element A[m]A[m]. If unequal, discard half the array and continue searching in the remaining sub-array of size ⌊n/2⌋\lfloor n/2 \rfloor.

BinarySearch(A[0..n-1], K):
    l = 0
    r = n - 1
    while l <= r do:
        m = floor((l + r) / 2)
        if K == A[m] return m
        else if K < A[m] r = m - 1
        else l = m + 1
    return -1

Watch the search window collapse. Each comparison discards half of what is left, which is why only a handful of steps are needed for a 13-element array:

Binary searchStep with the slider or ← →
BinarySearch(A[0..n-1], K):    l = 0    r = n - 1    while l <= r do:        m = floor((l + r) / 2)        if K == A[m] return m        else if K < A[m] r = m - 1        else l = m + 1    return -1
3142731394255707481859398

Searching a sorted array of 13 values for K = 70.

1 / 8
Cworst(n)=Cworst(⌊n/2⌋)+1for n>1,Cworst(1)=1C_{\text{worst}}(n) = C_{\text{worst}}(\lfloor n/2 \rfloor) + 1 \quad \text{for } n > 1, \quad C_{\text{worst}}(1) = 1 Cworst(n)=⌊log⁡2n⌋+1∈Θ(log⁡n)C_{\text{worst}}(n) = \lfloor \log_2 n \rfloor + 1 \in \Theta(\log n)

Fake-Coin Problem (Factor of 3)

Given nn identical-looking coins with 1 lighter fake coin and a balance scale:

  1. Divide nn coins into three piles: two equal piles of size ⌊n/3⌋\lfloor n/3 \rfloor and one pile with the remaining coins.
  2. Weigh the two equal piles:
    • If they balance: The fake coin is in the third pile.
    • If they don’t balance: The fake coin is in the lighter pile.
  3. Recurse on the pile containing the fake coin.
W(n)=W(⌈n/3⌉)+1  ⟹  W(n)=⌈log⁡3n⌉∈Θ(log⁡n)W(n) = W(\lceil n/3 \rceil) + 1 \implies W(n) = \lceil \log_3 n \rceil \in \Theta(\log n)

Ternary decrease requires strictly fewer weighings than binary halving because log⁡3n<log⁡2n\log_3 n < \log_2 n.


Russian Peasant Multiplication

Multiplies two positive integers nn and mm using only halving, doubling, and addition:

n×m={(n/2)×(2m)if n is even((n−1)/2)×(2m)+mif n is oddn \times m = \begin{cases} (n / 2) \times (2m) & \text{if } n \text{ is even} \\ ((n - 1) / 2) \times (2m) + m & \text{if } n \text{ is odd} \end{cases}

Example: 50×6550 \times 65

nn (halve and truncate)mm (double)Accumulate if nn is odd
5065-
25130+130+130
12260-
6520-
31040+1040+1040
12080+2080+2080
Total=130+1040+2080=3250(50×65=3250!)\text{Total} = 130 + 1040 + 2080 = 3250 \quad (50 \times 65 = 3250!)

2. Variable-Size-Decrease Algorithms

For an array sorted with uniformly distributed numerical keys, instead of probing the midpoint, estimate the key’s position using linear interpolation:

index=l+⌊K−A[l]A[r]−A[l](r−l)⌋index = l + \left\lfloor \frac{K - A[l]}{A[r] - A[l]} (r - l) \right\rfloor

Euclid’s Algorithm for Greatest Common Divisor (GCD)

Computes gcd⁡(m,n)\gcd(m, n) for non-negative integers where m≥nm \ge n:

gcd⁡(m,n)=gcd⁡(n,m mod n)with gcd⁡(m,0)=m\gcd(m, n) = \gcd(n, m \bmod n) \quad \text{with } \gcd(m, 0) = m

The decrease in argument size varies per step, but m mod n<m/2m \bmod n < m / 2 guaranteed every two steps.


Summary of Decrease-and-Conquer Variants

TypeClassic ExampleTypical RecurrenceEfficiency Class
Decrease-by-1Insertion Sort, Johnson-TrotterT(n)=T(n−1)+Θ(n)T(n) = T(n-1) + \Theta(n)Θ(n2)\Theta(n^2) or Θ(n!)\Theta(n!)
Decrease-by-factor-2Binary SearchT(n)=T(n/2)+1T(n) = T(n/2) + 1Θ(log⁡n)\Theta(\log n)
Decrease-by-factor-3Fake-Coin ProblemT(n)=T(n/3)+1T(n) = T(n/3) + 1Θ(log⁡3n)\Theta(\log_3 n)
Variable-SizeEuclid’s GCDT(m,n)=T(n,m mod n)T(m, n) = T(n, m \bmod n)Θ(log⁡n)\Theta(\log n)

Exercises

Exercise 1

Compute gcd⁡(31415,14142)\gcd(31415, 14142) step-by-step using Euclid’s algorithm and count the division steps.

Show solution ↓
Solution
  1. gcd⁡(31415,14142)  ⟹  31415 mod 14142=3131\gcd(31415, 14142) \implies 31415 \bmod 14142 = 3131
  2. gcd⁡(14142,3131)  ⟹  14142 mod 3131=1618\gcd(14142, 3131) \implies 14142 \bmod 3131 = 1618
  3. gcd⁡(3131,1618)  ⟹  3131 mod 1618=1513\gcd(3131, 1618) \implies 3131 \bmod 1618 = 1513
  4. gcd⁡(1618,1513)  ⟹  1618 mod 1513=105\gcd(1618, 1513) \implies 1618 \bmod 1513 = 105
  5. gcd⁡(1513,105)  ⟹  1513 mod 105=43\gcd(1513, 105) \implies 1513 \bmod 105 = 43
  6. gcd⁡(105,43)  ⟹  105 mod 43=19\gcd(105, 43) \implies 105 \bmod 43 = 19
  7. gcd⁡(43,19)  ⟹  43 mod 19=5\gcd(43, 19) \implies 43 \bmod 19 = 5
  8. gcd⁡(19,5)  ⟹  19 mod 5=4\gcd(19, 5) \implies 19 \bmod 5 = 4
  9. gcd⁡(5,4)  ⟹  5 mod 4=1\gcd(5, 4) \implies 5 \bmod 4 = 1
  10. gcd⁡(4,1)  ⟹  4 mod 1=0  ⟹  gcd⁡=1\gcd(4, 1) \implies 4 \bmod 1 = 0 \implies \gcd = 1.

The numbers are coprime (gcd⁡=1\gcd = 1). It took 10 division operations.

Exercise 2

Given 27 coins with 1 lighter fake coin, how many weighings on a balance scale are guaranteed to find the fake coin using the 3-pile method?

Show solution ↓
Solution

Using the ternary decrease-by-factor-3 approach:

  • Weighing 1: Divide 27 into three piles of 9 (9,9,99, 9, 9). Compare two piles →\to isolate the fake coin in a pile of 9.
  • Weighing 2: Divide 9 into three piles of 3 (3,3,33, 3, 3). Compare two piles →\to isolate the fake coin in a pile of 3.
  • Weighing 3: Divide 3 into three single coins (1,1,11, 1, 1). Compare two coins →\to locate the exact fake coin.

Answer: Exactly log⁡3(27)=3\log_3(27) = 3 weighings.