Leetcode 268: Missing Number
Given an array nums containing n distinct numbers, each in the range [0, n], return the only number in the range that is missing from the array.
Problem
Approach
Steps
Complexity
Input: The input consists of an array nums containing distinct integers in the range [0, n].
Example: For example, nums = [1, 0, 3].
Constraints:
• 1 <= n <= 10^4
• 0 <= nums[i] <= n
• All the numbers of nums are unique.
Output: The output is the only missing number in the range [0, n].
Example: For nums = [1, 0, 3], the output is 2.
Constraints:
• The missing number is guaranteed to be between 0 and n.
Goal: To find the missing number in the array, we can leverage a mathematical approach using XOR or summation properties.
Steps:
• 1. Calculate the XOR of all elements in the array.
• 2. XOR the result with all the numbers from 0 to n.
• 3. The result will be the missing number.
Goal: The input nums contains distinct numbers and is guaranteed to have exactly one missing number.
Steps:
• 1 <= n <= 10^4
• 0 <= nums[i] <= n
• All the numbers in nums are unique.
Assumptions:
• The array will always contain n distinct elements in the range [0, n].
• There will always be one missing number.
• Input: For nums = [1, 0, 3], the missing number is 2.
• Explanation: The array should have contained the numbers 0, 1, 2, and 3. The missing number is 2.
Approach: The approach to solve this problem involves leveraging the properties of XOR or the summation formula to find the missing number in the range [0, n].
Observations:
• The XOR of two identical numbers cancels out, which makes XOR an efficient way to find the missing number.
• We can use the XOR approach to achieve the solution in O(n) time and O(1) space.
Steps:
• 1. XOR all the elements in the array with the XOR of numbers from 0 to n.
• 2. The result of the XOR will be the missing number.
Empty Inputs:
• The array will always contain at least one element, so there are no empty inputs.
Large Inputs:
• For large inputs, the XOR approach ensures an O(n) time complexity and O(1) space complexity.
Special Values:
• If the array is missing 0, it will be correctly identified as the missing number.
Constraints:
• The solution should work for arrays of size up to 10^4.
int missingNumber(vector<int>& nums) {
int sum = nums[0];
for(int i = 1; i < nums.size(); i++)
sum ^= nums[i];
for(int i = 0; i <= nums.size(); i++)
sum ^= i;
return sum;
}
1 : Function Definition
int missingNumber(vector<int>& nums) {
Defines the `missingNumber` function that takes a vector of integers `nums` and returns the missing number from the range 0 to n.
2 : Initial XOR Setup
int sum = nums[0];
Initializes the variable `sum` with the first element of `nums` to start the XOR operation.
3 : Iterate Over Input Array
for(int i = 1; i < nums.size(); i++)
Iterates over the remaining elements in the `nums` array, starting from the second element.
4 : XOR Each Array Element
sum ^= nums[i];
Applies the XOR operation between the current `sum` and each element of the `nums` array.
5 : Iterate Over Full Range
for(int i = 0; i <= nums.size(); i++)
Iterates over the full range from 0 to `n`, where `n` is the size of the array.
6 : XOR with Range Elements
sum ^= i;
Applies the XOR operation between `sum` and each value in the range 0 to `n`.
7 : Return Missing Number
return sum;
Returns the result of the XOR operation, which is the missing number in the array.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n) since we iterate over the array once and perform constant time operations for each element.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is O(1) as we only use a constant amount of extra space.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus