
- 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 kth missing positive number in an array in Python
Suppose we have an array called nums with positive sorted strictly increasing values, and also have an integer k. We have to find the kth positive integer that is missing from this array.
So, if the input is like nums = [1,2,4,8,12], k = 6, then the output will be 10 because the missing numbers are [3,5,6,7,9,10,11], here the 6th term is 10.
To solve this, we will follow these steps −
nums := a new set from the elements present in nums
count := 0
num := 1
while count < k, do
if num is not in nums, then
count := count + 1
if count is same as k, then
return num
num := num + 1
return num
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums, k): nums = set(nums) count = 0 num = 1 while count < k: if num not in nums: count += 1 if count == k: return num num += 1 return num nums = [1,2,4,8,12] k = 6 print(solve(nums, k))
Input
[1,2,4,8,12], 6
Output
10
- Related Articles
- Program to find the kth missing number from a list of elements in Python
- Find the Smallest Positive Number Missing From an Unsorted Array
- Program to find first positive missing integer in range in Python
- 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
- Kth odd number in an array in C++
- First Missing Positive in Python
- Kth Largest Element in an Array in Python
- PHP program to find missing elements from an array
- Program to find lowest possible integer that is missing in the array in Python
- Program to find kth smallest element in linear time in Python
- PHP program to find the first ‘n’ numbers that are missing in an array
- Kth Largest Element in an Array
- Find four missing numbers in an array containing elements from 1 to N in Python
- Program to find Kth ancestor of a tree node in Python

Advertisements