The boundary initialization—low = max(0, k - n) and high = min(k, m)—is usually the trickiest part of this algorithm!

It feels counterintuitive because normally binary search starts at 0 and len(array). But here, i isn’t just an index into AA; i represents the exact count of elements we pick from AA to form the left half.

Here is the straightforward mathematical reason why this range is strictly necessary.

1. The Core Equation

To form the left half, we must pick ii elements from array AA and jj elements from array BB such that:

i+j=k  ⟹  j=k−ii + j = k \implies j = k - i

Now, we have physical limits on how many elements we can take from each array:

  1. Limits on ii (Array AA): You can’t take negative elements, and you can’t take more elements than AA actually has (mm).

    0≤i≤m0 \le i \le m

  2. Limits on jj (Array BB): Similarly, you can’t take negative elements from BB, and you can’t take more than BB actually has (nn).

    0≤j≤n0 \le j \le n

2. Deriving low and high

Since j=k−ij = k - i, we plug that into the second inequality (0≤j≤n0 \le j \le n):

0≤k−i≤n0 \le k - i \le n

Let’s break this double inequality down into two separate parts to isolate ii:

Part A: Solving for the upper bound (high)

  • From 0≤k−i0 \le k - i, adding ii to both sides gives:

    i≤ki \le k

  • Combined with our physical array bound (i≤mi \le m), ii cannot exceed either limit:

    high=min⁡(k,m)\text{high} = \min(k, m)

Intuition: If k=3k = 3, but array AA has 1010 elements (m=10m = 10), you would never take 4 or 5 elements from AA because you only need k=3k = 3 elements in total! So ii can never exceed kk.

Part B: Solving for the lower bound (low)

  • From k−i≤nk - i \le n, subtracting kk and flipping signs gives:

    i≥k−ni \ge k - n

  • Combined with our physical array bound (i≥0i \ge 0), ii cannot drop below either limit:

    low=max⁡(0,k−n)\text{low} = \max(0, k - n)

Intuition: Suppose k=8k = 8, but array BB only has 33 elements (n=3n = 3). Even if you took all 33 elements from BB, you are still short by 55 elements (8−3=58 - 3 = 5). Therefore, you MUST take at least 55 elements from AA. Taking i=0i = 0 or i=2i = 2 from AA would make it physically impossible to reach k=8k = 8.

3. Concrete Visual Examples

Example 1: Why low needs k - n

  • Array A (m=5m = 5): [10, 20, 30, 40, 50]

  • Array B (n=2n = 2): [1, 2]

  • k=6k = 6 (We want the 6th element overall)

If we naively set low = 0 and binary search tries i=1i = 1 (1 element from AA):

  • j=k−i=6−1=5j = k - i = 6 - 1 = 5.

  • But Array BB only has n=2n = 2 elements! Trying to access B[4]B[4] will cause an Index Out of Bounds crash or force negative elements.

Using the formula:

low=max⁡(0,6−2)=4\text{low} = \max(0, 6 - 2) = 4

high=min⁡(6,5)=5\text{high} = \min(6, 5) = 5

The search bounds for ii are restricted to [4, 5]. This guarantees we take at least 4 elements from AA, because BB can contribute at most 2 elements!

Example 2: Why high needs min(k, m)

  • Array A (m=10m = 10): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  • Array B (n=10n = 10): [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

  • k=2k = 2 (We want the 2nd element overall)

If we naively set high = m = 10 and binary search tries i=5i = 5 (5 elements from AA):

  • j=k−i=2−5=−3j = k - i = 2 - 5 = -3.

  • Taking −3-3 elements from Array BB makes no sense!

Using the formula:

low=max⁡(0,2−10)=0\text{low} = \max(0, 2 - 10) = 0

high=min⁡(2,10)=2\text{high} = \min(2, 10) = 2

The search bounds for ii are restricted to [0, 2]. We never try to take more than 2 elements from AA.

Quick Mental Model to Remember

Think of ii as a slider control for how many items you take from Array AA:

  • high = min(k, m): You can’t slide above mm (run out of elements in AA) and you shouldn’t slide above kk (you don’t need more than kk elements total).

  • low = max(0, k - n): You can’t slide below 00 (can’t take negative items) and you can’t slide below k−nk - n (otherwise BB wouldn’t have enough items to fill the rest of kk).

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon