Created
April 13, 2015 12:35
-
-
Save stephen-maina/6c7c8a5cb6c72d0ffaf4 to your computer and use it in GitHub Desktop.
MissingInteger Codility Lesson 2
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.stream.IntStream; | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
if(A.length==1){ | |
return A[0]+1; | |
} | |
int[] sorted=IntStream.of(A).sorted().toArray(); | |
int temp=sorted[0]; | |
for(int index=1;index<sorted.length;index++){ | |
if(temp<sorted[index]&&temp+1==sorted[index]){ | |
temp=sorted[index]; | |
}else if(temp==sorted[index]){ | |
continue; | |
}else{ | |
return sorted[index-1]+1; | |
} | |
} | |
return A[A.length-1]+1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment