Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 511 Bytes

Missing Number.md

File metadata and controls

27 lines (20 loc) · 511 Bytes

Algorithm

Use Gauss's formula: The sum of 1 to n is (n)(n+1)/2

Solution

class Solution {
    public int missingNumber(int[] nums) {
        int expectedSum = nums.length * (nums.length + 1) / 2;
        int actualSum = 0;
        for (int num : nums) {
            actualSum += num;
        }
        return expectedSum - actualSum;
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links