Skip to content

Instantly share code, notes, and snippets.

@yangbrian
Last active December 16, 2015 19:29
Show Gist options
  • Select an option

  • Save yangbrian/5485715 to your computer and use it in GitHub Desktop.

Select an option

Save yangbrian/5485715 to your computer and use it in GitHub Desktop.
Number 10 on the Practice AP
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println(mystery(0, arr.length - 1, 0));
}
private static int[] arr = {1, 2, 3, 314, 1337};
public static int mystery(int low, int high, int num) {
int mid = (low + high)/2;
if (low > high)
return low;
else if (arr[mid] < num)
return mystery(mid + 1, high, num);
else if (arr[mid] > num)
return mystery(low, mid - 1, num);
else
return mid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment