DEV Community

Cover image for Day 83 of 100 days dsa coding challenge
Manasi Patil
Manasi Patil

Posted on

Day 83 of 100 days dsa coding challenge

Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! πŸ’»πŸ”₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! πŸš€

100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney

Problem:
https://www.geeksforgeeks.org/problems/kth-missing-positive-number-in-a-sorted-array/1

Kth Missing Positive Number in a Sorted Array

*Difficulty: Medium Accuracy: 53.02% *

Given a sorted array of distinct positive integers arr[], You need to find the kth positive number that is missing from the arr[].
Examples:
Input: arr[] = [2, 3, 4, 7, 11], k = 5
Output: 9
Explanation: Missing are 1, 5, 6, 8, 9, 10… and 5th missing number is 9.
Input: arr[] = [1, 2, 3], k = 2
Output: 5
Explanation: Missing are 4, 5, 6… and 2nd missing number is 5.

Input: arr[] = [3, 5, 9, 10, 11, 12], k = 2
Output: 2
Explanation: Missing are 1, 2, 4, 6… and 2nd missing number is 2.
Constraints:
1 ≀ arr.size() ≀ 105
1 ≀ k ≀ 105
1 ≀ arr[i] ≀ 106

Solution:
class Solution:
def kthMissing(self, arr, k):
l, r = 0, len(arr) - 1
while l <= r:
mid = (l + r) // 2
if arr[mid] - (mid + 1) < k:
l = mid + 1
else:
r = mid - 1
return l + k

Top comments (0)