coding

Created 27 Jul 2023

Binary Search: an Intuitive Algorithm

Everyone has performed a binary search without realizing: if you look for the word "gerontology" in the dictionary, you wouldn't go flip through every page until you found it. Instead you'd check the middle, and if you overshot then you'd check the middle of the first chunk, then if you if undershot you'd check the middle of the remaining chunk, and so on, until you find the word.

In programmer terms, we can use binary search to search for an item in a sorted array. We keep track of a left pointer and a right poiner. Then we check the middle index by (left + right) / 2 (rounding down).

For example, if we were wanted to find the index of 8 in the array [1, 2, 5, 7, 8, 9, 10], we'd start by setting the left and right pointer to index 0 and index 6 respectively.

[1, 2, 5, 7, 8, 9, 10]
 ^        ^        ^
left=0   mid=3   right=6

Then, since the item we're looking for (8) is larger than the item at the middle, we know that it has to lie to the right of the middle pointer, thus we can update the left pointer to be mid+1.

[1, 2, 5, 7, 8, 9, 10]
             ^     ^
             l=4   r=6

Now the middle pointer is at index 5, and points to 9. This is more than 8, so we can update the right pointer to be mid-1:

[1, 2, 5, 7, 8, 9, 10]
             ^
             l=r=4

Now the left and right pointer point to the same thing, so we've found the index of 8: it's 4. Well actually, we need one more check that the item that's being pointed to is actually 8 - for example, if it was 7.5 instead, we'd still end up with left = right = 4.

The time complexity of binary search is O(logn)O(\log n) , because each comparison halves the search space, so it takes a logarithmic number of operations (and log2(n)=lognlog2\log_2(n) = \frac{\log n}{\log 2}).

Pseudocode

I like to define the left pointer as the one you know it's definitely greater than or equal to, and the right pointer as the one you know it's definitely less than. So, lx<rl \leq x \lt r.

function search(arr, length, target)
    l := 0
    r := n-1
    while l+1 < r do
        m := floor((l+r) / 2)
        if arr[m] > target then
            r := m
        else if arr[m] < target then
            l := m+1
        else
            return m
        end
    end
end

Example problem: Minimum excludant

Given a sorted array of distinct positive integers, find the smallest positive integer that is not in the array.

Examples

Input: [1, 2, 3, 5, 9, 12, 13]

Output: 4

Input: [3, 5, 7, 10]

Output: 1

Input: [1, 2, 3, 4]

Output: 5

Solution

Harder problem: Ntarsis' Set

View problem on codeforces

Ntarsis has been given a set SS, initially containing the integers 1,2,3,1010001, 2, 3 \cdots, 10^{1000} in sorted order. Every day, he removes the a1a_1-th, a2a_2-th, \cdots, ana_n-th smallest numbers in SS simultaneously.

What is the smallest element in SS after kk days?


Input

The first line contains the number of testcases t  (1t105)t \;(1 \leq t \leq 10^5). The description of the testcases follows.

The first line of each testcase consists of two integers nn and kk (1n,k21051 \leq n, k \leq 2 \cdot 10^5) - the length of aa and the number of days.

The following line of each testcase consists of nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ai1091 \leq a_i \leq 10^9) - the elements of array aa.

It is guaranteed that:

  • the sum of nn over all testcases does not exceed 21052 \cdot 10^5
  • the sum of kk over all testcases does not exceed 21052 \cdot 10^5
  • a1<a2<<ana_1 \lt a_2 \lt \cdots \lt a_n for all testcases.


Output

For each testcase, print an integer that is the smallest element in SS after kk days.

Example

Input:

7
5 1
1 2 4 5 6
5 3
1 3 5 6 7
4 1000
2 3 4 5
9 1434
1 4 7 9 12 15 17 18 20
10 4
1 3 5 7 9 11 13 15 17 19
10 6
1 4 7 10 13 16 19 22 25 28
10 150000
1 3 4 5 10 11 12 13 14 15

Output:

3
9
1
12874
16
18
1499986

Solution