Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Created June 28, 2016 14:37
Show Gist options
  • Save tiagopereira17/74f8095077dfa5748417be9aba71dba4 to your computer and use it in GitHub Desktop.
Save tiagopereira17/74f8095077dfa5748417be9aba71dba4 to your computer and use it in GitHub Desktop.
Find the minimal positive integer not occurring in a given sequence.
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