Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created April 12, 2015 13:47
Show Gist options
  • Select an option

  • Save stephen-maina/dc8c22cb85d6b3bbc1df to your computer and use it in GitHub Desktop.

Select an option

Save stephen-maina/dc8c22cb85d6b3bbc1df to your computer and use it in GitHub Desktop.
Codility PermMissingElem Solution
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