We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f59f871 commit 6100099Copy full SHA for 6100099
Java/missing-number.java
@@ -2,9 +2,11 @@
2
* Time Complexity - O(n)
3
* Space Complexity - O(1)
4
*
5
-* Using sum of first n natural numbers formula.
+* Please comment one of the method if you are submitting solution directly
6
*/
7
class Solution {
8
+
9
+ // Using sum of first n natural numbers formula.
10
public int missingNumber(int[] nums) {
11
int numsLen = nums.length;
12
@@ -21,4 +23,17 @@ public int missingNumber(int[] nums) {
21
23
// subtract actualSum from expectedSum
22
24
return expectedSum-actualSum;
25
}
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
39
0 commit comments