Skip to content

Instantly share code, notes, and snippets.

@szagriichuk
Created September 13, 2015 10:47
Show Gist options
  • Save szagriichuk/cbda0a4557c8e25a8f2e to your computer and use it in GitHub Desktop.
Save szagriichuk/cbda0a4557c8e25a8f2e to your computer and use it in GitHub Desktop.
Missing Number
public class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int n = 0;
for(int i = 0; i < nums.length; i++, n++){
if(n !=nums[i])
return n;
}
return nums[nums.length-1]+1;
}
}
public class Solution {
public int missingNumber(int[] nums) {
int max = findMax(nums);
int result = 0;
for(int i = 0; i < max; i++){
if(i < nums.length){
result = result ^ i+1 ^ nums[i];
}else {result=result ^ i+1;}
}
return result;
}
int findMax(int[] nums){
int max = nums[0];
for(int i = 1; i < nums.length; i++){
if(max < nums[i]){
max = nums[i];
}
}
return max == nums.length-1 ? ++max: max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment