Kth Missing Positive Number in a Sorted Array (original) (raw)
Last Updated : 11 Dec, 2024
Given a **sorted array of **distinct positive integers **arr[] and integer k, the task is to find the **kth positive number that is missing from 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, 7, 8, 13,... and 2nd missing number is 2.
Table of Content
- [Naive Approach] Using Hash Set - O(n) Time and O(n) Space
- [Better Approach] Using Index Comparison - O(n) Time and O(1) Space
- [Expected Approach] Using Binary Search - O(log n) Time and O(1) Space
[Naive Approach] Using Hash Set - O(n) Time and O(n) Space
- Insert all array elements into a hash set.
- Now starting from 1, one by one search all natural numbers in the hash set and increment the missing count whenever a number is not present.
- When the count of missing numbers become k, return the current number. C++ `
// C++ Program to find Kth missing positive number // using Hash Set
#include #include #include using namespace std;
int kthMissing(vector& arr, int k) {
// Insert all array elements into a set
unordered_set<int> s(arr.begin(), arr.end());
int count = 0, curr = 0;
while (count < k) {
curr++;
// Increment missing count if current
// element is missing
if (s.find(curr) == s.end()) {
count++;
}
}
return curr;
}
int main() { vector arr = {2, 3, 4, 7, 11}; int k = 5; cout << kthMissing(arr, k); return 0; }
Java
// Java Program to find Kth missing positive number // using Hash Set
import java.util.HashSet; import java.util.Set;
class GfG {
// Function to find the k-th missing positive number
static int kthMissing(int[] arr, int k) {
// Insert all array elements into a set
Set<Integer> set = new HashSet<>();
for (int num : arr) {
set.add(num);
}
int count = 0, curr = 0;
// Loop until we find the k-th missing number
while (count < k) {
curr++;
// Increment missing count if current
// element is missing
if (!set.contains(curr)) {
count++;
}
}
return curr;
}
public static void main(String[] args) {
int[] arr = {2, 3, 4, 7, 11};
int k = 5;
System.out.println(kthMissing(arr, k));
}
}
Python
Python Program to find Kth missing positive number
using Hash Set
def kthMissing(arr, k):
# Insert all array elements into a set
s = set(arr)
count = 0
curr = 0
# Loop until we find the k-th missing number
while count < k:
curr += 1
# Increment missing count if current
# element is missing
if curr not in s:
count += 1
return curr
if name == "main": arr = [2, 3, 4, 7, 11] k = 5 print(kthMissing(arr, k))
C#
// C# Program to find Kth missing positive number // using Hash Set
using System; using System.Collections.Generic;
class GfG {
// Function to find the k-th missing positive number
static int KthMissing(int[] arr, int k) {
// Insert all array elements into a HashSet
HashSet<int> set = new HashSet<int>(arr);
int count = 0, curr = 0;
// Loop until we find the k-th missing number
while (count < k) {
curr++;
// Increment missing count if current
// element is missing
if (!set.Contains(curr))
count++;
}
return curr;
}
static void Main() {
int[] arr = { 2, 3, 4, 7, 11 };
int k = 5;
// Print the k-th missing number
Console.WriteLine(KthMissing(arr, k));
}
}
JavaScript
// JavaScript Program to find Kth missing positive number // using Hash Set
function kthMissing(arr, k) {
// Insert all array elements into a set
let s = new Set(arr);
let count = 0;
let curr = 0;
// Loop until we find the k-th missing number
while (count < k) {
curr++;
// Increment missing count if current element is missing
if (!s.has(curr)) {
count++;
}
}
return curr;
}
// Driver code let arr = [2, 3, 4, 7, 11]; let k = 5; console.log(kthMissing(arr, k));
`
[Better Approach] Using Index Comparison - O(n) Time and O(1) Space
The idea is based on the following facts
- The value of the result would be at least **k. For example, for [10, 11, 12] and k = 5, the answer would be 5 and for [1, 2, 3] and k = 2, the answer would be **5.
- The maximum possible value of k would be **k + n where **n is size of the array. This happen for arrays of first **n natural numbers like [1, 2, 3, 4] and [1, 2, 3]
- While traversing the array, if arr[i] becomes greater than (k + i), then the **k-th missing element is **k + i.
For example, for [1, 3, 5] and k = 2.
- For i = 0, we have arr[i] less than 2 + i.
- For i = 1, we have arr[i] equal to 2 + i.
- For i = 2, we have arr[i] more than 2 + i. So we return 2 + i which is 4. C++ `
// C++ Program to find Kth missing positive number // using index comparison
#include #include using namespace std;
int kthMissing(vector &arr, int k) { int n = arr.size(); for (int i = 0; i < n; i++) { if (arr[i] > (k + i)) return (k + i); }
// If all numbers from 1 to n are present
// in arr[], return k + n
return k + n;
}
int main() { vector arr = {2, 3, 4, 7, 11}; int k = 5; cout << kthMissing(arr, k); return 0; }
Java
// Java Program to find Kth missing positive number // using index comparison
import java.util.*;
class GfG { static int kthMissing(int[] arr, int k) {
int n = arr.length;
for (int i = 0; i < n; i++) {
if (arr[i] > (k + i)) {
return (k + i);
}
}
// If all numbers from 1 to n are present
// in arr[], return k + n
return k + n;
}
public static void main(String[] args) {
int[] arr = {2, 3, 4, 7, 11};
int k = 5;
System.out.println(kthMissing(arr, k));
}
}
Python
Python Program to find Kth missing positive number
using index comparison
def kthMissing(arr, k): n = len(arr) for i in range(n): if arr[i] > (k + i): return k + i
# If all numbers from 1 to n are present
# in arr[], return k + n
return k + n
if name == 'main': arr = [2, 3, 4, 7, 11] k = 5 print(kthMissing(arr, k))
C#
// C# Program to find Kth missing positive number // using index comparison
using System; using System.Collections.Generic;
class GfG { static int KthMissing(int[] arr, int k) { int n = arr.Length; for (int i = 0; i < n; i++) { if (arr[i] > (k + i)) { return k + i; } }
// If all numbers from 1 to n are present
// in arr[], return k + n
return k + n;
}
static void Main() {
int[] arr = {2, 3, 4, 7, 11};
int k = 5;
Console.WriteLine(KthMissing(arr, k));
}
}
JavaScript
// JavaScript Program to find Kth missing positive // number using index comparison
function kthMissing(arr, k) { let n = arr.length; for (let i = 0; i < n; i++) { if (arr[i] > (k + i)) { return k + i; } }
// If all numbers from 1 to n are present
// in arr[], return k + n
return k + n;
}
// Driver code let arr = [2, 3, 4, 7, 11]; let k = 5; console.log(kthMissing(arr, k));
`
[Expected Approach] Using Binary Search - O(log n) Time and O(1) Space
In the previous approach, we used linear search to find the first index where **arr[i] > (k + i). Since the input array is sorted, once we have found the index i such that arr[i] > (k + i), then for all indices j (j > i), arr[j] will also be greater than (k + j). So, we can optimize the previous approach using binary search to find the index i so that the **k-th missing element is **k + i.
C++ `
// C++ Program to find Kth missing positive number // using Binary Search
#include #include using namespace std;
// Function to find the k-th missing positive number int kthMissing(vector &arr, int k) { int lo = 0, hi = arr.size() - 1; int res = arr.size() + k;
// Binary Search for index where arr[i] > (i + k)
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] > mid + k) {
res = mid + k;
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return res;
}
int main() { vector arr = {2, 3, 4, 7, 11}; int k = 5; cout << kthMissing(arr, k); return 0; }
Java
// Java Program to find Kth missing positive number // using Binary Search
import java.util.*;
class GfG {
// Function to find the k-th missing positive number
static int kthMissing(int[] arr, int k) {
int lo = 0, hi = arr.length - 1;
int res = arr.length + k;
// Binary Search for index where arr[i] > (i + k)
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] > mid + k) {
res = mid + k;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 3, 4, 7, 11};
int k = 5;
System.out.println(kthMissing(arr, k));
}
}
Python
Python Program to find Kth missing positive number
using Binary Search
Function to find the k-th missing positive number
def kthMissing(arr, k): lo = 0 hi = len(arr) - 1 res = len(arr) + k
# Binary Search for index where arr[i] > (i + k)
while lo <= hi:
mid = (lo + hi) // 2
if arr[mid] > mid + k:
res = mid + k
hi = mid - 1
else:
lo = mid + 1
return res
if name == "main": arr = [2, 3, 4, 7, 11] k = 5 print(kthMissing(arr, k))
C#
// C# Program to find Kth missing positive number // using Binary Search
using System;
class GfG { static int kthMissing(int[] arr, int k) { int lo = 0, hi = arr.Length - 1; int res = arr.Length + k;
// Binary Search for index where arr[i] > (i + k)
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] > mid + k) {
res = mid + k;
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return res;
}
static void Main() {
int[] arr = { 2, 3, 4, 7, 11 };
int k = 5;
Console.WriteLine(kthMissing(arr, k));
}
}
JavaScript
// JavaScript Program to find Kth missing positive number // using Binary Search
// Function to find the k-th missing positive number function kthMissing(arr, k) { let lo = 0, hi = arr.length - 1; let res = arr.length + k;
// Binary Search for index where arr[i] > (i + k)
while (lo <= hi) {
let mid = Math.floor((lo + hi) / 2);
if (arr[mid] > mid + k) {
res = mid + k;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return res;
}
// Driver Code const arr = [2, 3, 4, 7, 11]; const k = 5; console.log(kthMissing(arr, k));
`