

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 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 Questions & Answers
- Program to find the kth missing number from a list of elements in Python
- Program to find first positive missing integer in range in Python
- First Missing Positive 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
- PHP program to find missing elements from an array
- Kth odd number in an array in C++
- Kth Largest Element in an Array in Python
- Missing Number in Python
- Kth Largest Element in 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
- Java program to find the largest number in an array
- Java program to find the smallest number in an array
- Find four missing numbers in an array containing elements from 1 to N in Python
Advertisements