Created
November 24, 2015 08:57
-
-
Save kuanyingchou/c354d553ce2c7cc531fa to your computer and use it in GitHub Desktop.
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
import java.util.HashSet; | |
class NotInArray { | |
public static void main(String[] args) { | |
int[] input = new int[] { Integer.MAX_VALUE, 3, 4, 7, 9, Integer.MIN_VALUE }; | |
System.out.println(notInArray(input)); | |
} | |
private static int notInArray(int[] input) { | |
HashSet<Integer> possibleValues = new HashSet<>(); | |
for(int i = 0; i<input.length; i++) { | |
int current = input[i]; | |
if(current > Integer.MIN_VALUE && !possibleValues.contains(current-1)) | |
possibleValues.add(current-1); | |
if(current < Integer.MAX_VALUE && !possibleValues.contains(current+1)) | |
possibleValues.add(current+1); | |
if(possibleValues.contains(current)) possibleValues.remove(current); | |
} | |
if(possibleValues.size() > 0) return possibleValues.iterator().next(); | |
else throw new RuntimeException("Oh oh!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment