🧠
Brute Force (Linear Scan Counting Missing)
💡 This approach exists to build intuition by simulating the counting of missing numbers one by one, which is straightforward but inefficient.
Intuition
We iterate from 1 upwards, checking if each number is in the array. If not, we count it as missing until we reach the kth missing number.
Algorithm
- Initialize a pointer for the array and a counter for missing numbers.
- Iterate from 1 upwards, checking if current number matches array element.
- If it matches, move array pointer forward; else increment missing count.
- When missing count reaches k, return the current number.
💡 This algorithm is easy to understand but inefficient because it checks every number one by one.
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 number is not in array
if missing_count == kReturn current number when kth missing 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 currentReturn 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 missing count
int current = 1Start from smallest positive integer
if (i < n && arr[i] == current)Check if current number is in array
missingCount++Increment missing count if current missing
if (missingCount == k) return currentReturn kth missing number
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 currentReturn kth missing number
We potentially iterate up to k missing numbers plus array traversal. If k is large, this is inefficient.
💡 For n=5 and k=5, this means up to 10 iterations, which is fine for small inputs but slow for large k.
Interview Verdict: TLE for large k, but good for understanding problem
This approach is too slow for large inputs but helps understand the problem and counting missing numbers.