Skip to content

Commit

Permalink
Merge pull request #170 from Abhi-sheKkK/find_missing_number
Browse files Browse the repository at this point in the history
Find missing number from array in python
  • Loading branch information
abhisek247767 authored Oct 31, 2024
2 parents 7e09e45 + b9d0eba commit 3d85335
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/find_missing_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
Question : We are given an array with consecutive natural numbers , with one number missing
We have to return that number .
Example : Input = a =[1,2,3,4,5,6,8,9,10]
output = 7
Approach : There exists a very simple approach to this problem . But , overthinking can mess it up,
Sum= (n(n+1))/2 is the formula for sum of n natural numbers , if we subtract then sum of
elements of the given array with this then the result will be the remaining number .
where , n = len(a)
'''
class Solution:
def missingNumber(self, nums: list[int]) -> int:
n = len(nums)
expected_sum = n * (n + 1) // 2
actual_sum = sum(nums)
return expected_sum - actual_sum

0 comments on commit 3d85335

Please sign in to comment.