Given sets A, B, C of size n each and integer k, determine if there exist a, b, c (a ∈ A, b ∈ B, c ∈ C) such that
a + b + c = k
Divide and Conquer Algorithm:
Divide If n = 1, directly check if a + b + c = k
If n > 1, split A, B, C into halves: A1, A2 = split(A) B1, B2 = split(B) C1, C2 = split(C)
Conquer Recursively solve 8 subproblems:
T(A1, B1, C1, k)
T(A1, B1, C2, k)
T(A1, B2, C1, k)
T(A1, B2, C2, k)
T(A2, B1, C1, k)
T(A2, B1, C2, k)
T(A2, B2, C1, k)
T(A2, B2, C2, k)
If any returns true, return true. Else return false.
Combine Return result of conquer step.
The recurrence is T(n) = 8T(n/2) + O(n) which solves to O(n^2) by Master's theorem.
As a result, examining all 8 options in a recursive fashion solves the issue in optimal O(n^2) time. Due to halving, the recursion tree depth decreases quickly, resulting in an efficient algorithm.