-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_268.java
31 lines (28 loc) · 947 Bytes
/
_268.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions.firstthousand;
public class _268 {
public static class Solution1 {
/*
* we could take advantage of the array indices
* then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up.
*/
public int missingNumber(int[] nums) {
int xor = 0;
int i = 0;
for (; i < nums.length; i++) {
xor ^= i ^ nums[i];
}
return xor ^ i;
}
}
public static class Solution2 {
public int missingNumber(int[] nums) {
int n = nums.length;
long sum = n + (n * n - n) / 2; // this is the formula to compute the sum for arithmetic
// sequence
for (int i = 0; i < nums.length; i++) {
sum -= nums[i];
}
return (int) sum;
}
}
}