🧠
Brute Force (Linear Scan)
💡 This approach exists to build intuition by directly simulating the missing numbers. It is simple but inefficient, helping beginners understand the problem before optimizing.
Intuition
Check every positive integer starting from 1, count how many are missing from the array until we reach the kth missing.
Algorithm
- Initialize a pointer i for the array and a counter for missing numbers.
- Iterate from 1 upwards, checking if the current number matches arr[i].
- If it matches, move i forward; else increment missing count.
- When missing count reaches k, return the current number.
💡 This algorithm is straightforward but inefficient because it checks every number one by one, which is easy to understand but slow for large inputs.
def findKthPositive(arr, k):
missing_count = 0
current = 1
i = 0
n = len(arr)
while True:
if i < n and arr[i] == current:
i += 1
else:
missing_count += 1
if missing_count == k:
return current
current += 1
# Driver code
if __name__ == '__main__':
print(findKthPositive([2,3,4,7,11], 5)) # Output: 9
Line Notes
missing_count = 0Initialize count of missing numbers found so far
current = 1Start checking from the smallest positive integer
if i < n and arr[i] == currentCheck if current number is present in array
missing_count += 1Increment missing count when current is not in array
if missing_count == kReturn current when kth missing number is found
public class Solution {
public static int findKthPositive(int[] arr, int k) {
int missingCount = 0, current = 1, i = 0, n = arr.length;
while (true) {
if (i < n && arr[i] == current) {
i++;
} else {
missingCount++;
if (missingCount == k) {
return current;
}
}
current++;
}
}
public static void main(String[] args) {
System.out.println(findKthPositive(new int[]{2,3,4,7,11}, 5)); // 9
}
}
Line Notes
int missingCount = 0Initialize missing count to zero
int current = 1Start checking from 1
if (i < n && arr[i] == current)Check if current number is in array
missingCount++Increment missing count if current is missing
if (missingCount == k)Return current when kth missing found
#include <iostream>
#include <vector>
using namespace std;
int findKthPositive(vector<int>& arr, int k) {
int missingCount = 0, current = 1, i = 0, n = arr.size();
while (true) {
if (i < n && arr[i] == current) {
i++;
} else {
missingCount++;
if (missingCount == k) {
return current;
}
}
current++;
}
}
int main() {
vector<int> arr = {2,3,4,7,11};
cout << findKthPositive(arr, 5) << endl; // 9
return 0;
}
Line Notes
int missingCount = 0Initialize count of missing numbers
int current = 1Start from smallest positive integer
if (i < n && arr[i] == current)Check if current is in array
missingCount++Increment missing count if current missing
if (missingCount == k)Return current when kth missing found
function findKthPositive(arr, k) {
let missingCount = 0, current = 1, i = 0, n = arr.length;
while (true) {
if (i < n && arr[i] === current) {
i++;
} else {
missingCount++;
if (missingCount === k) {
return current;
}
}
current++;
}
}
// Test
console.log(findKthPositive([2,3,4,7,11], 5)); // 9
Line Notes
let missingCount = 0Initialize missing count
let current = 1Start checking from 1
if (i < n && arr[i] === current)Check if current number is in array
missingCount++Increment missing count if current missing
if (missingCount === k)Return current when kth missing found
We potentially iterate up to k missing numbers and scan through the array with pointer i. For large k, this is inefficient.
💡 If k=10^5, this means up to 100,000 iterations, which is slow but still feasible; for larger k, it becomes impractical.
Interview Verdict: TLE for large inputs
This approach is too slow for large inputs but helps understand the problem before optimizing.