
Problem
Solution
Submissions
Missing Number
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a C program that finds the missing number in an array containing n distinct numbers taken from the range 0 to n. The array should contain all the numbers in the range except one number which is missing.
Example 1
- Input: nums = [3, 0, 1]
- Output: 2
- Explanation:
We have an array with numbers from the range 0 to 3, with one number missing.
The array contains [3, 0, 1], so the missing number must be from the range 0 to 3.
The numbers 0, 1, and 3 are present, but 2 is not in the array.
Therefore, the missing number is 2.
Example 2
- Input: nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
- Output: 8
- Explanation:
We have an array with numbers from the range 0 to 9, with one number missing.
The array contains [9, 6, 4, 2, 3, 5, 7, 0, 1], so the missing number must be from the range 0 to 9.
After checking each number, we find that 8 is not present in the array.
Therefore, the missing number is 8.
Constraints
- The array size will be n, and the range of numbers will be from 0 to n
- The array will contain exactly n distinct numbers from the range 0 to n+1
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use the mathematical formula for the sum of numbers from 0 to n
- Calculate the expected sum of all numbers from 0 to n
- Calculate the actual sum of the numbers in the array
- The difference between the expected sum and the actual sum is the missing number
- You can also consider using XOR operations for an elegant solution