
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find four missing numbers in an array containing elements from 1 to N in Python
Suppose we have an array of distinct numbers where each number lies in the range [1, N], the array size is (N-4) and no single element is repeated. So, we can understand four numbers, from 1 to N, are missing in the array. We have to find these 4 missing numbers in sorted manner.
So, if the input is like A =[2, 8, 4, 13, 6, 11, 9, 5, 10], then the output will be [1, 3, 7, 12]
To solve this, we will follow these steps −
temp_arr := an array of size 4 with all 0s
for i in range 0 to size of A, do
temp := |A[i]|
if temp <= size of A , then
A[temp - 1] := A[temp - 1] *(-1)
otherwise when temp > size of A , then
if temp mod size of A is non-zero, then
temp_arr[temp mod size of A - 1] := -1
otherwise,
temp_arr[(temp mod size of A) +size of A - 1] := -1
for i in range 0 to size of A, do
if A[i] > 0, then
display i + 1
for i in range 0 to size of temp_arr, do
if temp_arr[i] >= 0, then
display size of A + i + 1
Example
Let us see the following implementation to get better understanding −
def find_missing_nums(A) : temp_arr = [0]*4 for i in range(0,len(A)) : temp = abs(A[i]) if (temp <= len(A)) : A[temp - 1] = A[temp - 1] * (-1) elif (temp > len(A)) : if (temp % len(A)) : temp_arr[temp % len(A) - 1] = -1 else : temp_arr[(temp % len(A)) +len(A) - 1] = -1 for i in range(0, len(A) ) : if (A[i] > 0) : print((i + 1) , end=" ") for i in range(0, len(temp_arr)) : if (temp_arr[i] >= 0) : print((len(A) + i + 1) , end=" ") A = [2, 8, 4, 13, 6, 11, 9, 5, 10] find_missing_nums(A)
Input
[2, 8, 4, 13, 6, 11, 9, 5, 10]
Output
1 3 7 12
- Related Articles
- Find four missing numbers in an array containing elements from 1 to N in C++
- Program to find all missing numbers from 1 to N in Python
- PHP program to find missing elements from an array
- Returning an array containing last n even numbers from input array in JavaScript
- PHP program to find the first ‘n’ numbers that are missing in an array
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- How to find the missing number in a given Array from number 1 to n in Java?
- Program to find missing numbers from two list of numbers in Python
- Find missing elements in List in Python
- Find missing element in a sorted array of consecutive numbers in Python
- Program to find kth missing positive number in an array in Python
- Program to create a list with n elements from 1 to n in Python
- Program to find the kth missing number from a list of elements in Python
- Finding missing element in an array of numbers in JavaScript
- Program to find maximum product of two distinct elements from an array in Python
