DAY 21 of PYTHON top 100 questions : from Basic to Advanced !!

DAY 21 of PYTHON top 100 questions : from Basic to Advanced !!

Write a Python program to find the missing number in a given array of integers.

array = [1,2,3,4,5,7,8,9]
n = len(array) + 1
total_sum = (n * (n + 1)) // 2
array_sum = sum(array)
missing_number = total_sum - array_sum
print(missing_number)

Output :

6

The code calculates the missing number in a given array of integers. It does so by finding the sum of integers from 1 to n, where n is the length of the array plus one. Then, it calculates the sum of the given array. The missing number is obtained by subtracting the array sum from the total sum of integers. Finally, the code prints the missing number.


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!