R06_Handout
R06_Handout
Problem 1 Efficient integer multiplication. Given two n-bit numbers x, y (i.e. we represent x, y using base 2, long strings
of zeros and ones), how do we efficiently compute their product xy? What immediately comes to our mind is probably brute
force, where we multiply x by each digits of y to obtain partial products, shift each partial product, and compute the sum.
For example, 123 · 456 = 123 · 400 + 123 · 50 + 123 · 6. This clearly gives a naı̈ve runtime of Θ(n2 ). Can we do better?
(1) What if we split the n-bit x into two n/2-bit binary numbers x+ , x− , where x+ are the top n/2 MSBs (most significant
bits) and x− the bottom n/2 LSBs (least significant bits), and likewise for y + and y − ? Then,
x · y = (2n/2 x+ + x− )(2n/2 y + + y − ).
Unfortunately, this seemingly beautiful divide-and-conquer based approach does not improve the asymptotic runtime.
Prove that this is the case. Can you explain what caused this?
(2) In 1960, Andrey Kolmogorov 1 conjectured that any integer multiplication task would require Ω(n2 ) elementary oper-
ations, and he brought this up in a seminar. It took Anatoly Karatsuba, then a 23-year-old studnet of his, only one
week to disprove this conjecture. As a comic relief, Kolmogorov was excited, and proceed to publish a paper under the
name of Karatsuba but never told him. Karatsuba would later have to find out about this himself! Karatsuba realized
that the arithmetic operations performed on splitted x+ , x− , y + , y − can be reused to reduce the number of recursive
calls via the following:
x+ y − + x− y + = (x+ + x− )(y + + y − ) − x+ y + − x− y − , (*)
Based on this identity, design an alternate algorithm to computing xy that achieves an asymptotic runtime better than
Θ(n2 ). Justify your algorithm runtime.
120th century Soviet mathematical giant who made major contributions to literally every, single, branch of mathematics. If you are a math
major, you will see his name in literally every advanced math textbook.
2
Problem 2 Median of Two Sorted Arrays. This is one of the most classic LeetCode problems and (probably) frequently
asked in job interviews, so you may want to have it ready.
Given two sorted arrays A[] and B[] of length m, n, provide a O(log m + log n) algorithm that returns the median of the
combined array. Note that you cannot use a two-pointer approach to incrementally find the (m + n)/2-th smallest element,
for that would result in linear time. Given the time constraint, you need only provide a high-level description of
the algorithm; no need to worry about, e.g., the exact indices. Also, there are many different approaches, and we briefly
discuss the two most popular ones below.
(1) Let us first assume m = n, so the two arrays are of equal length. Can you think of a Divide & Conquer based algorithm
to solve this problem?
One starting point would be the following. Consider splitting A[] into two halves: A[0 : n/2] and A[n/2 : n], and
likewise for B[]. Now we have four sorted subarrays of length n/2. Can you prove that we can discard two out of these
four subarrays, such that the overall median of the other two subarrays equals the median of the two original arrays
that the problem asks us to find? How would you use this to define a subproblem to run Divide & Conquer on?
(2) Now, to take one step further, let us drop the assumption that m = n. How would your proof change, if any?
(3) (Optional reading; digression from Divide & Conquer) If one is not extremely well-versed with Divide & Conquer, it is
not very likely that they could come up with the above solution in a timed interview. But we are not out of options!
We can also tackle this problem using an old-fashioned binary search, once we know the constraints. The smallest
(m + n)/2 elements from the combined array A[] + B[] can also be decomposed back into a subarray of A[] and another
of B[]. To phrase the problem as a binary search problem, we want to know the exact length of this subarray of A[],
and we can perform binary search on this length.
Can you come up with a binary search based algorithm to solve this problem?