MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / missingNumber

Method missingNumber

Java/missing-number.java:10–25  ·  view source on GitHub ↗
(int[] nums)

Source from the content-addressed store, hash-verified

8
9 // Using sum of first n natural numbers formula.
10 public int missingNumber(int[] nums) {
11 int numsLen = nums.length;
12
13 // sum of n natural number is
14 // Sn = n * (n+1)/2
15 int expectedSum = numsLen*(numsLen+1)/2;
16
17 // calculate actual sum
18 int actualSum = 0;
19 for (int i=0; i<numsLen; i++){
20 actualSum += nums[i];
21 }
22
23 // subtract actualSum from expectedSum
24 return expectedSum-actualSum;
25 }
26
27 // Using XOR approach
28 public int missingNumber(int[] nums) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected