
Problem
Solution
Submissions
Missing Number in an Array
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 8
Write a C# program to find the missing number in an array containing n distinct numbers taken from 0 to n. The array may be in any order.
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 since it does not appear in nums.
Example 2
- 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 since it does not appear in nums.
Constraints
- n == nums.length
- 1 ≤ n ≤ 10^4
- 0 ≤ nums[i] ≤ n
- All the numbers of nums are unique
- 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 first n natural numbers: n*(n+1)/2
- Find the sum of the given array
- The difference between the expected sum and actual sum is the missing number
- Alternatively, you can use bit manipulation with XOR operations