- Related Questions & Answers
- Find missing elements in List in Python
- PHP program to find missing elements from an array
- Python program to find N largest elements from a list
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Python program to find sum of elements in list
- Python program to find the largest number in a list
- Python program to find the smallest number in a list
- Program to find maximum sum of popped k elements from a list of stacks in Python
- Find sum of elements in list in Python program
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Python program to remove Duplicates elements from a List?
- Find kth node from Middle towards Head of a Linked List in C++
- C# Program to find the cube of elements in a list
- Python program to find the second largest number in a list

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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))

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

9

Advertisements