A missing number is an element that should be in the sequence but isn’t, while a repeated number appears more than once instead of the missing one.
Key takeaways:
The missing number in an array of distinct numbers from the range can be found using the XOR operation, which cancels out all existing numbers, leaving only the missing one.
The algorithm runs in O(n) time and uses O(1) extra space, making it an optimal solution for this problem.
This problem has real-world relevance in areas like database index reconstruction and sequence alignment in bioinformatics, where data integrity is critical.
In the field of algorithmic challenges, the missing number problem on LeetCode is a great way to learn more about how to work with lists and find things within them. This kind of problem is especially important in situations where data integrity is important, such as database index reconstruction or sequence alignment in bioinformatics.
Given an array, nums
, containing distinct numbers in the range , return the only number in the range that is missing from the array.
Constraints
nums.length
nums[i]
Let’s gain a better understanding of the problem with the following examples:
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Missing Number
What will be the output if this array is given as input?
11
2
1
3
To find the missing number in an array containing distinct numbers taken from the range , we use an efficient algorithm that uses the properties of the XOR operation. The algorithm follows these key steps:
We start by initializing a variable, missing
, to the length of the input array nums
. This value represents , the upper bound of the range from which numbers are taken, and ensures that all numbers from to are included in the XOR operation.
We iterate through nums
, and in each iteration, we update the missing variable by applying the XOR operation with the index of the current element and its corresponding value. The result of this operation is saved in missing
. The XOR operation is applied to each index and each element, ensuring that every number in the complete range is XORed exactly once.
After iterating through nums
, we return missing
, which holds the value of the missing number. This is because the XOR operation cancels out all numbers that appear twice (once as an index and once as an array element), leaving only the missing number.
This algorithm works efficiently due to the properties of the XOR operation: any number XORed with itself results in , and any number XORed with remains unchanged. By XORing all indexes and all elements, all present numbers cancel out, and the remaining value is the missing number.
Let’s look at the following illustration for a better understanding:
Let’s look at the code for the algorithm we just discussed.
def find_missing_number(nums):# Initialize the missing number with the length of the arraymissing = len(nums)# Iterate over the arrayfor i in range(len(nums)):# Update the missing number by XORing the current index,# the current element and the missing variablemissing ^= i ^ nums[i]return missingdef main():inputnumbers = [[4, 0, 3, 1],[8, 3, 5, 2, 4, 6, 0, 1],[1, 2, 3, 4, 6, 7, 8, 9, 10, 5],[0],[1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23]]i = 1for x in inputnumbers:print(i, ".\tnums: ", x, sep = "")print("\n\tMissing number: ", find_missing_number(x), sep = "")print("-"*100, "\n", sep = "")i+=1if __name__ == "__main__":main()
Now, let’s look at the time and space complexity of the solution.
The time complexity of this solution isnums
.
The space complexity of this solution is
Accelerate your coding career with The Coding Career Handbook. Master job-winning strategies, career growth tactics, and unconventional advice tailored for early-career software engineers.
Haven’t found what you were looking for? Contact Us
Free Resources