#t542. 二分查找模版

二分查找模版

Binary Search Template

Problem Description

Given a sorted strictly-increasing array (no duplicate elements), use binary search to find the position of the value xx. If xx is not in the array, output -1.

Input Format

The first line contains an integer nn, the number of array elements (n≤106n \le 10^6).

The second line contains nn numbers, the nn strictly-increasing array elements (1≤1\le element value ≤108\le 10^8).

The third line contains an integer xx, the number to search for (0≤x≤1080\le x\le 10^8).

Output Format

The (1-based) position of xx in the array, or −1-1.

Sample Input 1

10
1 3 5 7 9 11 13 15 17 19
3

Sample Output 1

2

Hint

Try to implement binary search both recursively and iteratively.