bikas.rath
BAN USERWhy you need a HashSet if it can be done using a boolean array of size n say bool[]
Iterate over the number array once and find the minimum say it is k
Set bool[0] (Think it represents the minimum number in the array) to true and rest to false.
Iterate over the number array again if num[i]-k < n then set bool[num[i]-k] true.
Check index of first false index in bool array. Say the index is j.
The number is j+k
public int minNonArrayNum(num[]){
int size = num.length;
boolean bool[] = new boolean[size];
bool[0] = true;
int minimum = num[0];
for (int i = 0 ; i < size ; i++) {
if (num[i] < minimum) {
minimum = num[i];
}
}
for (int i = 0 ; i < size ; i++) {
if (num[i]-minimum < size) {
bool[num[i]-minimum] = true;
}
}
for (int i = 1 ; i < size ; i++) {
if (!bool[i]) {
return i+minimum;
}
}
}
Ignore my previous 2 redundant comments. I submitted the answer multiple times because of network issues. :)
- bikas.rath June 05, 2015