
Problem Statement
In this task, you're provided with an array called nums
which contains n
unique numbers taken from a sequential range from 0
to n
. This means that the array should ideally have n + 1
numbers, but since it contains only n
numbers, it's evident that exactly one number from this range is missing. The challenge is to determine which number in the range [0, n]
is absent 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`.
Constraints
n == nums.length
1 <= n <= 104
0 <= nums[i] <= n
- All the numbers of
nums
are unique.
Approach and Intuition
To find the missing number efficiently, consider the properties of the range and the constraints given:
- Since the array
nums
contains every number once and only one number from the sequence is missing, the sum of the sequence0
ton
(the ideal sum) can be compared against the sum of numbers in the array. - The formula for the sum of the first
n
natural numbers is ( \frac{n \times (n + 1)}{2} ). This gives the sum of the sequence[0, n]
. - Calculate the actual sum of elements in the
nums
array. - The difference between the ideal sum and the actual sum of the array gives the missing number.
Example illustrations from the provided samples:
Example 1:
- Input:
nums = [3,0,1]
- Calculation:
- Ideal sum for
n = 3
is ( \frac{3 \times (3 + 1)}{2} = 6 ). - Actual sum of array = 3 + 0 + 1 = 4.
- Missing number = 6 - 4 = 2.
- Ideal sum for
- Hence, the output is 2.
- Input:
Example 2:
- Input:
nums = [0,1]
- Calculation:
- Ideal sum for
n = 2
is ( \frac{2 \times (2 + 1)}{2} = 3 ). - Actual sum of array = 0 + 1 = 1.
- Missing number = 3 - 1 = 2.
- Ideal sum for
- Hence, the output is 2.
- Input:
Example 3:
- Input:
nums = [9,6,4,2,3,5,7,0,1]
- Calculation:
- Ideal sum for
n = 9
is ( \frac{9 \times (10)}{2} = 45 ). - Actual sum of array = 37.
- Missing number = 45 - 37 = 8.
- Ideal sum for
- Hence, the output is 8.
- Input:
Using this approach efficiently identifies the missing number, adhering to the constraints and making sure to handle up to 10,000 elements in the array by direct calculations.
Solutions
- Java
- Python
class Solution {
public int findMissing(int[] elements) {
int idealSum = elements.length * (elements.length + 1) / 2;
int sum = 0;
for (int value : elements) sum += value;
return idealSum - sum;
}
}
This Java solution addresses the problem of finding a missing number in an array of integers from 0 to n. The approach taken involves computing the sum of a complete sequence of numbers from 0 to n and then subtracting the sum of the numbers present in the array. This discrepancy directly gives the missing number, as shown below:
- Calculate the 'idealSum' which is the sum of all numbers from 0 to n using the formula ( \frac{n(n+1)}{2} ).
- Initialize a variable 'sum' to hold the cumulative value of the array elements.
- Loop through the array using an enhanced for loop, adding each element's value to 'sum'.
- Finally, compute the missing number by subtracting 'sum' from 'idealSum'.
The function returns the result, effectively pinpointing the missing number in the sequence. This technique relies on mathematical principles to avoid unnecessary complexity and ensures efficient computation with a direct approach.
class Solution:
def findMissing(self, numbers):
total_nums = len(numbers)
expected_total = total_nums * (total_nums + 1) // 2
sum_of_numbers = sum(numbers)
return expected_total - sum_of_numbers
The provided solution in Python resolves the problem of finding a missing number in a list of integers ranging from 0 to n. Here is a concise breakdown of how the solution works:
- Calculate
total_nums
, the length of the provided list, which indicates how many numbers (excluding the missing number) are in the list. - Determine
expected_total
, the sum of a complete sequence of numbers from 0 to n. This value is calculated using the formula for the sum of an arithmetic series:n(n+1)/2
. - Compute
sum_of_numbers
, the sum of elements actually present in the list. - Subtract
sum_of_numbers
fromexpected_total
to find the missing number in the sequence.
This method efficiently identifies the missing element with a time complexity of O(n) and does not require additional space, making it highly effective for this type of problem.
No comments yet.