Use Gauss's formula: The sum of 1 to n is (n)(n+1)/2
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 Complexity: O(n)
- Space Complexity: O(1)