Amazon Interview Question
SDE-3sCountry: United States
private static boolean evaluateConsecutive(int[] arr){
Set set = new TreeSet();
Arrays.stream(arr).boxed().forEach(x -> set.add(x));
int a=-1,b=-1;
Iterator<Integer> it = set.iterator();
do {
a = (it.hasNext()) ? it.next() : -1;
b = (it.hasNext()) ? it.next() : -1;
if (b > 0 && b != a+1){
return false;
}
} while (it.hasNext());
return true;
}
I would hashset the array to remove duplicate items. Then find the min and max value. If (max-min) + 1 == hashset size then the array is contiguous, otherwise it is not.
- Dung Nguyen April 17, 2019