- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find all missing numbers from 1 to N in Python

Suppose we have a list of numbers called nums of size n where all numbers in the list are present in the interval [1, n] some elements may appear twice while others only once. We have to find all of the numbers from [1, n] such that they are not in the list. We have to return the numbers sorted in ascending order. We have to try to find a solution that takes linear time and constant space.

So, if the input is like [4, 4, 2, 2, 6, 6], then the output will be [1, 3, 5].

To solve this, we will follow these steps −

- arr := an array of size nums + 1, and fill with 0
- for each i in nums, do
- arr[i] := arr[i] + 1

- missing := a new list
- for i in range 0 to size of arr, do
- if arr[i] is same as 0 and i is not same as 0, then
- insert i at the end of missing

- if arr[i] is same as 0 and i is not same as 0, then
- return missing

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums): arr = [0]*(len(nums)+1) for i in nums: arr[i] += 1 missing = [] for i in range(len(arr)): if arr[i] == 0 and i != 0: missing.append(i) return missing ob = Solution() print(ob.solve([4, 4, 2, 2, 6, 6]))

## Input

[4, 4, 2, 2, 6, 6]

## Output

[1, 3, 5]

- Related Articles
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Find four missing numbers in an array containing elements from 1 to N in Python
- Program to find missing numbers from two list of numbers in Python
- Java Program to Display All Prime Numbers from 1 to N
- Swift Program to Display All Prime Numbers from 1 to N
- Kotlin Program to Display All Prime Numbers from 1 to N
- Haskell program to display all prime numbers from 1 to n
- Find four missing numbers in an array containing elements from 1 to N in C++
- Program to find all upside down numbers of length n in Python
- Python program to count total set bits in all number from 1 to n.
- How to Display all Prime Numbers from 1 to N in Golang?
- Compute sum of digits in all numbers from 1 to n
- PHP program to find the first ‘n’ numbers that are missing in an array
- Python program to print all Disarium numbers between 1 to 100
- Smallest possible number divisible by all numbers from 1 to n in JavaScript

Advertisements