Created
June 28, 2016 14:37
-
-
Save tiagopereira17/74f8095077dfa5748417be9aba71dba4 to your computer and use it in GitHub Desktop.
Find the minimal positive integer not occurring in a given sequence.
This file contains 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 MissingInteger { | |
public int minMissingInteger(int[] A) { | |
if(A == null || A.length == 0) { | |
return -1; | |
} | |
int[] items = new int[A.length + 1]; | |
for(int i = 0; i < items.length; i++) { | |
items[i] = -1; | |
} | |
for(int i = 0; i < A.length; i++) { | |
int item = A[i]; | |
if(item > 0 && item <= (A.length + 1)) { | |
items[item - 1] = 1; | |
} | |
} | |
for(int i = 0; i < items.length; i++) { | |
if(items[i] == -1) { | |
return i + 1; | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment