- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find first positive missing integer in range in Python
Suppose we have a list of sorted list of distinct integers of size n, we have to find the first positive number in range [1 to n+1] that is not present in the array.
So, if the input is like nums = [0,5,1], then the output will be 2, as 2 is the first missing number in range 1 to 5.
To solve this, we will follow these steps −
target := 1
for each i in arr, do
if i is same as target, then
target := target + 1
return target
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, arr): target = 1 for i in arr: if i == target: target += 1 return target ob = Solution() nums = [0,5,1] print(ob.solve(nums))
Input
[0,5,1]
Output
2
- Related Articles
- First Missing Positive in Python
- Program to find kth missing positive number in an array in Python
- Program to find lowest possible integer that is missing in the array in Python
- Find missing numbers in a sorted list range in Python
- Python program to reverse bits of a positive integer number?
- Find missing elements of a range in C++
- Program to express a positive integer number in words in C++
- Print first k digits of 1/n where n is a positive integer in C Program
- PHP program to find the first ‘n’ numbers that are missing in an array
- Find the one missing number in range using C++
- Program to find bitwise AND of range of numbers in given range in Python
- Python program to find missing and additional values in two lists?
- Write a program in C++ to find the missing positive number in a given array of unsorted integers
- Write a program in Java to find the missing positive number in a given array of unsorted integers
- Program to find all missing numbers from 1 to N in Python

Advertisements