Last active
December 16, 2015 19:29
-
-
Save yangbrian/5485715 to your computer and use it in GitHub Desktop.
Number 10 on the Practice AP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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