SDE Path

Binary Search on a Sorted Array

Easy

Binary Search on a Sorted Array

Implement classic binary search to find the index of a target value in a sorted array.

Input format

The first line contains N and the target. The second line contains N distinct integers in sorted (increasing) order.

Output format

Print the 0-based index of the target, or -1 if it is not present.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
6 9
1 3 5 9 11 14
Expected output
3
Example 2
Input
5 7
1 3 5 9 11
Expected output
-1