Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6100099

Browse files
authoredAug 16, 2020
Update missing-number.java
1 parent f59f871 commit 6100099

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed
 

‎Java/missing-number.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
* Time Complexity - O(n)
33
* Space Complexity - O(1)
44
*
5-
* Using sum of first n natural numbers formula.
5+
* Please comment one of the method if you are submitting solution directly
66
*/
77
class Solution {
8+
9+
// Using sum of first n natural numbers formula.
810
public int missingNumber(int[] nums) {
911
int numsLen = nums.length;
1012

@@ -21,4 +23,17 @@ public int missingNumber(int[] nums) {
2123
// subtract actualSum from expectedSum
2224
return expectedSum-actualSum;
2325
}
26+
27+
// Using XOR approach
28+
public int missingNumber(int[] nums) {
29+
int numsLen = nums.length;
30+
31+
int xor = numsLen;
32+
33+
for(int i=0; i<numsLen; i++){
34+
xor ^= nums[i] ^ i;
35+
}
36+
37+
return xor;
38+
}
2439
}

0 commit comments

Comments
 (0)
Please sign in to comment.