w3resource

Python: Find the missing number in a given array of numbers between 10 and 20


24. Find Missing Number in an Array of Numbers Between 10 and 20

Write a Python program to find the missing number in a given array of numbers between 10 and 20.

Sample Solution-1:

Python Code:

import array as arr
def test(nums):
    return sum(range(10, 21)) - sum(list(nums))

array_num = arr.array('i', [10, 11, 12, 13, 14, 16, 17, 18, 19, 20])
print("Original array:")
for i in range(len(array_num)):    
    print(array_num[i], end=' ')
print("\nMissing number in the said array (10-20): ",test(array_num))
 
array_num = arr.array('i', [10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
print("\nOriginal array:")
for i in range(len(array_num)):    
    print(array_num[i], end=' ')
print("\nMissing number in the said array (10-20): ",test(array_num))

Sample Output:

Original array:
10 11 12 13 14 16 17 18 19 20 
Missing number in the said array (10-20):  15

Original array:
10 11 12 13 14 15 16 17 18 19 
Missing number in the said array (10-20):  20

Flowchart:

Flowchart: Find the missing number in a given array of numbers between 10 and 20

Sample Solution-2:

Python Code:

import array as arr
def test(nums):
    return 165 - sum(list(nums))

array_num = arr.array('i', [10, 11, 12, 13, 14, 16, 17, 18, 19, 20])
print("Original array:")
for i in range(len(array_num)):    
    print(array_num[i], end=' ')
print("\nMissing number in the said array (10-20): ",test(array_num))
 
array_num = arr.array('i', [10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
print("\nOriginal array:")
for i in range(len(array_num)):    
    print(array_num[i], end=' ')
print("\nMissing number in the said array (10-20): ",test(array_num))

Sample Output:

Original array:
10 11 12 13 14 16 17 18 19 20 
Missing number in the said array (10-20):  15

Original array:
10 11 12 13 14 15 16 17 18 19 
Missing number in the said array (10-20):  20

Flowchart:

Flowchart: Remove all duplicate elements from a given array

For more Practice: Solve these Related Problems:

  • Write a Python program to identify the missing number in a sorted array by computing the difference between the expected sum and the actual sum.
  • Write a Python program to use set operations to find the missing number in an array representing a continuous range.
  • Write a Python program to iterate over a range from 10 to 20 and return the number that is not present in the array.
  • Write a Python program to implement a function that takes an array and returns the missing element in a consecutive range, or a default value if none is missing.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to remove all duplicate elements from a given array and returns a new array.
Next: Python Conditional Statements and Loops Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.