Created
April 12, 2015 13:47
-
-
Save stephen-maina/dc8c22cb85d6b3bbc1df to your computer and use it in GitHub Desktop.
Codility PermMissingElem Solution
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
| import java.util.Arrays; | |
| import java.util.stream.IntStream; | |
| import java.util.stream.Collectors; | |
| class Solution { | |
| public int solution(int[] A) { | |
| // write your code in Java SE 8 | |
| int first=0; | |
| int last=A.length; | |
| if(last==first){ | |
| return 1; | |
| } | |
| int [] sorted=IntStream.of(A).sorted().toArray(); | |
| for(int index=0;index<A.length;index++){ | |
| int check=index+1; | |
| if(!sort(sorted,first,last,check)){ | |
| return check; | |
| } | |
| } | |
| return A.length+1; | |
| } | |
| private boolean sort(int[] L,int low,int high,int k){ | |
| int mid = (low + high) / 2; | |
| if (mid>L.length||low > high) { | |
| return false; | |
| } else if (L[mid] == k) { | |
| return true; | |
| } else if (L[mid] < k) { | |
| return sort(L, mid + 1, high, k); | |
| } else { | |
| return sort(L, low, mid - 1, k); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment