//Xralier
import java.util.Scanner;
public class BinarySearch {
public static int binarySearch (int ar[] , int key)
{
int low = 0;
int high = ar.length - 1;
while ( low <= high )
{
int mid = (low + high) / 2;
if ( ar [mid] > key )
{ high = mid - 1; }
else if ( ar [mid] < key )
{ low = mid + 1; }
else
return mid;
}
return -1 ;
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter Maximum array elements : ");
int n = in.nextInt();
int ar [] = new int[n];
System.out.println("Enter array elements : ");
for (int i = 0 ; i < n ; i++)
{
ar[i]= in.nextInt();
}
System.out.println("Enter Key Value to Search : ");
int k = in.nextInt();
int res = binarySearch(ar,k);
if (res>=0)
{
System.out.println("Value found at index : " + res);
}
if (res<0)
{
System.out.println("Value not found ");
}
}
}