-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathFind afixed point in given array.java
More file actions
39 lines (35 loc) · 1.16 KB
/
Find afixed point in given array.java
File metadata and controls
39 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*Given an array of n distinct integers sorted in ascending order, write a function that returns a Fixed Point in the array,
if there is any Fixed Point present in array, else returns -1.
Fixed Point in an array is an index i such that arr[i] is equal to i.
Note that integers in array can be negative. */
//Solution
// Java program to check fixed point
// in an array using binary search
class Main
{
static int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
/* low + (high - low)/2; */
int mid = (low + high)/2;
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is
no Fixed Point */
return -1;
}
//main function
public static void main(String args[])
{
int arr[] = {-10, -1, 0, 3 , 10, 11, 30, 50, 100};
int n = arr.length;
System.out.println("Fixed Point is "
+ binarySearch(arr,0, n-1));
}
}