Description
Given an array nums
containing n
distinct numbers in the range [0, n]
, return the only number in the range that is missing from the array.
Examples:
Example 1:
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
Solution in Python
Python
from typing import List
class Solution:
def missingNumber(self, nums: List[int]) -> int:
# Calculate the expected sum of numbers in the range [0, n]
# This is based on the formula for the sum of the first n natural numbers: n*(n+1)/2
n = len(nums) # The length of the input array gives the value of n
expected_sum = n * (n + 1) // 2 # The expected sum of numbers from 0 to n
# Calculate the actual sum of the numbers in the input array
actual_sum = sum(nums)
# The missing number will be the difference between the expected sum and the actual sum
return expected_sum - actual_sum
Detailed Explanation:
- Input and Problem: You are given an array
nums
containingn
distinct numbers from the range [0, n]. There is one missing number, and the task is to find that missing number. - Approach:
- Sum Formula: The sum of the first
n
natural numbers is given by the formula: Sum=n×(n+1)/2. This formula gives the expected sum of numbers from 0 ton
. - Actual Sum: Compute the sum of the elements present in the array
nums
. - Difference: The missing number will be the difference between the expected sum and the actual sum of the numbers in the array.
- Sum Formula: The sum of the first
- Efficiency:
- Time complexity:
O(n)
because we only iterate through the array once to compute the sum. - Space complexity:
O(1)
as we only use a constant amount of extra space.
- Time complexity:
Solution in C++
C++
#include <vector>
using namespace std;
class Solution {
public:
int missingNumber(vector<int>& nums) {
// Get the size of the input array, which is n
int n = nums.size();
// Calculate the expected sum of numbers in the range [0, n]
// This is based on the sum formula: n*(n+1)/2
int expected_sum = n * (n + 1) / 2;
// Calculate the actual sum of the numbers in the input array
int actual_sum = 0;
for (int num : nums) {
actual_sum += num;
}
// The missing number is the difference between the expected sum and the actual sum
return expected_sum - actual_sum;
}
};
Solution in Javascript
JavaScript
var missingNumber = function(nums) {
// Get the length of the input array, which is n
const n = nums.length;
// Calculate the expected sum of numbers in the range [0, n]
// The formula for the sum of the first n numbers is: n * (n + 1) / 2
const expectedSum = n * (n + 1) / 2;
// Calculate the actual sum of the numbers in the input array
let actualSum = 0;
for (let i = 0; i < nums.length; i++) {
actualSum += nums[i];
}
// The missing number is the difference between the expected sum and the actual sum
return expectedSum - actualSum;
};
Solution in Java
Java
class Solution {
public int missingNumber(int[] nums) {
// Get the length of the input array, which is n
int n = nums.length;
// Calculate the expected sum of numbers in the range [0, n]
// The formula for the sum of the first n natural numbers is: n * (n + 1) / 2
int expectedSum = n * (n + 1) / 2;
// Calculate the actual sum of the numbers in the input array
int actualSum = 0;
for (int num : nums) {
actualSum += num;
}
// The missing number is the difference between the expected sum and the actual sum
return expectedSum - actualSum;
}
}