Skip to content

Instantly share code, notes, and snippets.

@anil477
Last active June 20, 2017 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/06d0a747bb413972c9fcdb0bfb4301ce to your computer and use it in GitHub Desktop.
Save anil477/06d0a747bb413972c9fcdb0bfb4301ce to your computer and use it in GitHub Desktop.
// Given an array of n distinct integers sorted in ascending order, write a function that returns a Fixed Point in the array
// http://www.geeksforgeeks.org/find-a-fixed-point-in-a-given-array/
class FixedPoint {
public static void main(String[] args) {
int[] arr={0, 3, 7};
fixed(arr);
}
public static void fixed(int[] a)
{
int l = 0;
int h = a.length-1;
int mid = -1;
while(l<=h){
mid = (l+h)/2;
//System.out.println(" Mid " + mid + " High " + h + " low " + l);
if(a[mid] == mid)
{
System.out.println(" Fixed " + mid);
return;
}
if( a[mid] > mid)
{
//System.out.println("Shift Left");
h = mid - 1;
} else {
//System.out.println("Shift Right");
l = mid + 1;
}
}
//System.out.println(" Mid " + mid + " High " + h + " low " + l);
System.out.println(" No Results ");
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment