fix: incorrect range detection in find_missing_number (#10361)

* Fix incorrect range detection in find_missing_number

* Support consecutive decreasing numbers

Added support for consecutive decreasing numbers in the find_missing_number function.

* Support unordered numbers
This commit is contained in:
Manpreet Singh 2023-10-15 10:15:44 +05:30 committed by GitHub
parent c9ba5e1b6f
commit 3ecad36f92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,11 +11,18 @@ def find_missing_number(nums: list[int]) -> int:
Example:
>>> find_missing_number([0, 1, 3, 4])
2
>>> find_missing_number([1, 3, 4, 5, 6])
2
>>> find_missing_number([6, 5, 4, 2, 1])
3
>>> find_missing_number([6, 1, 5, 3, 4])
2
"""
n = len(nums)
missing_number = n
low = min(nums)
high = max(nums)
missing_number = high
for i in range(n):
missing_number ^= i ^ nums[i]
for i in range(low, high):
missing_number ^= i ^ nums[i - low]
return missing_number