- 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 the kth missing number from a list of elements in Python

Suppose we have a list of sorted unique numbers called nums and an integer k, we have to find the kth missing number from the first element of the given list.

So, if the input is like nums = [5,6,8,10,11], k = 1, then the output will be 9, as 9 is the second (index 1) missing number.

To solve this, we will follow these steps −

for i in range 1 to size of nums, do

diff := nums[i] - nums[i - 1] - 1

if k >= diff, then

k := k - diff

otherwise,

return nums[i - 1] + k + 1

return nums[-1] + k + 1

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums, k): for i in range(1, len(nums)): diff = nums[i] - nums[i - 1] - 1 if k >= diff: k -= diff else: return nums[i - 1] + k + 1 return nums[-1] + k + 1 ob = Solution() nums = [5,6,8,10,11] k = 1 print(ob.solve(nums, k))

## Input

[5,6,8,10,11], 1

## Output

9

- Related Questions & Answers
- Program to find kth missing positive number in an array in Python
- Python program to find the character position of Kth word from a list of strings
- Find missing elements in List in Python
- Program to find missing numbers from two list of numbers in Python
- Program to find duplicate item from a list of elements in Python
- PHP program to find missing elements from an array
- Python program to find N largest elements from a list
- Program to find sum of odd elements from list in Python
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Program to find largest kth index value of one list in Python
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find out the XOR values of specific elements from a generated list in Python
- Python program to find sum of elements in list
- Program to find the kth factor of n using Python

Advertisements