Given an array of size n and a number k, we need to print first k natural numbers that are not there in the given array.
Examples:
Input : [2 3 4]
k = 3
Output : [1 5 6]
Input : [-2 -3 4]
k = 2
Output : [1 2]
- Sort the given array.
- After sorting, we find the position of the first positive number in the array.
- Now we traverse the array and keep printing elements in gaps between two consecutive array elements.
- If gaps don’t cover k missing numbers, we print numbers greater than the largest array element.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printKMissing( int arr[], int n, int k)
{
sort(arr, arr + n);
int i = 0;
while (i < n && arr[i] <= 0)
i++;
int count = 0, curr = 1;
while (count < k && i < n) {
if (arr[i] != curr) {
cout << curr << " " ;
count++;
}
else
i++;
curr++;
}
while (count < k) {
cout << curr << " " ;
curr++;
count++;
}
}
int main()
{
int arr[] = { 2, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 3;
printKMissing(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static void printKMissing( int [] arr, int n, int k)
{
Arrays.sort(arr);
int i = 0 ;
while (i < n && arr[i] <= 0 )
i++;
int count = 0 , curr = 1 ;
while (count < k && i < n) {
if (arr[i] != curr) {
System.out.print(curr + " " );
count++;
}
else
i++;
curr++;
}
while (count < k) {
System.out.print(curr + " " );
curr++;
count++;
}
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 4 };
int n = arr.length;
int k = 3 ;
printKMissing(arr, n, k);
}
}
|
Python3
def printKMissing(arr, n, k) :
arr.sort()
i = 0
while (i < n and arr[i] < = 0 ) :
i = i + 1
count = 0
curr = 1
while (count < k and i < n) :
if (arr[i] ! = curr) :
print ( str (curr) + " " , end = '')
count = count + 1
else :
i = i + 1
curr = curr + 1
while (count < k) :
print ( str (curr) + " " , end = '')
curr = curr + 1
count = count + 1
arr = [ 2 , 3 , 4 ]
n = len (arr)
k = 3
printKMissing(arr, n, k);
|
C#
using System;
class GFG {
static void printKMissing( int [] arr,
int n,
int k)
{
Array.Sort(arr);
int i = 0;
while (i < n && arr[i] <= 0)
i++;
int count = 0, curr = 1;
while (count < k && i < n) {
if (arr[i] != curr) {
Console.Write(curr + " " );
count++;
}
else
i++;
curr++;
}
while (count < k) {
Console.Write(curr + " " );
curr++;
count++;
}
}
public static void Main()
{
int [] arr = {2, 3, 4};
int n = arr.Length;
int k = 3;
printKMissing(arr, n, k);
}
}
|
PHP
<?php
function printKMissing( $arr , $n , $k )
{
sort( $arr ); sort( $arr , $n );
$i = 0;
while ( $i < $n && $arr [ $i ] <= 0)
$i ++;
$count = 0; $curr = 1;
while ( $count < $k && $i < $n ) {
if ( $arr [ $i ] != $curr ) {
echo $curr , " " ;
$count ++;
}
else
$i ++;
$curr ++;
}
while ( $count < $k ) {
echo $curr , " " ;
$curr ++;
$count ++;
}
}
$arr = array ( 2, 3, 4 );
$n = sizeof( $arr );
$k = 3;
printKMissing( $arr , $n , $k );
?>
|
Javascript
<script>
function printKMissing(arr, n, k) {
arr.sort((a, b) => a - b);
let i = 0;
while (i < n && arr[i] <= 0)
i++;
let count = 0, curr = 1;
while (count < k && i < n) {
if (arr[i] != curr) {
document.write(curr + " " );
count++;
}
else
i++;
curr++;
}
while (count < k) {
document.write(curr, " " );
curr++;
count++;
}
}
let arr = new Array(2, 3, 4);
let n = arr.length;
let k = 3;
printKMissing(arr, n, k);
</script>
|
Time Complexity: O(n Log n)
Auxiliary Space: O(1)
Alternative Method:
- We can use hashmap to search in O(1) time.
- Use a dictionary to store values in the array.
- We run a loop from 1 to n+k and check whether they are in hashmap.
- If they are not present print the number.
- if all k elements are found break the loop.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printmissingk( int arr[],
int n, int k)
{
map< int , int > d;
for ( int i = 0; i < n; i++)
d[arr[i]] = arr[i];
int cnt = 1;
int fl = 0;
for ( int i = 0; i < (n + k); i++)
{
if (d.find(cnt) == d.end())
{
fl += 1;
cout << cnt << " " ;
if (fl == k)
break ;
}
cnt += 1;
}
}
int main()
{
int arr[] = {1, 4, 3};
int n = sizeof (arr) /
sizeof (arr[0]);
int k = 3;;
printmissingk(arr, n, k);
}
|
Java
import java.io.*;
import java.util.HashMap;
class GFG
{
static void printmissingk( int arr[], int n, int k)
{
HashMap<Integer, Integer> d = new HashMap<>();
for ( int i = 0 ; i < n; i++)
d.put(arr[i], arr[i]);
int cnt = 1 ;
int fl = 0 ;
for ( int i = 0 ; i < (n + k); i++) {
if (!d.containsKey(cnt)) {
fl += 1 ;
System.out.print(cnt + " " );
if (fl == k)
break ;
}
cnt += 1 ;
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 3 };
int n = arr.length;
int k = 3 ;
printmissingk(arr, n, k);
}
}
|
Python3
def printmissingk(arr,n,k):
d = {}
for i in range ( len (arr)):
d[arr[i]] = arr[i]
cnt = 1
fl = 0
for i in range (n + k):
if cnt not in d:
fl + = 1
print (cnt,end = " " )
if fl = = k:
break
cnt + = 1
print ()
arr = [ 1 , 4 , 3 ]
n = len (arr)
k = 3
printmissingk(arr,n,k)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void printmissingk( int [] arr, int n, int k)
{
Dictionary< int , int > d = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
d.Add(arr[i], arr[i]);
}
int cnt = 1;
int fl = 0;
for ( int i = 0; i < (n + k); i++)
{
if (!d.ContainsKey(cnt))
{
fl += 1;
Console.Write(cnt + " " );
if (fl == k)
break ;
}
cnt += 1;
}
}
static public void Main (){
int [] arr = { 1, 4, 3 };
int n = arr.Length;
int k = 3;
printmissingk(arr, n, k);
}
}
|
Javascript
<script>
function printmissingk(arr,n,k)
{
let d = new Map();
for (let i = 0; i < n; i++)
d.set(arr[i], arr[i]);
let cnt = 1;
let fl = 0;
for (let i = 0; i < (n + k); i++) {
if (!d.has(cnt)) {
fl += 1;
document.write(cnt + " " );
if (fl == k)
break ;
}
cnt += 1;
}
}
let arr=[1, 4, 3];
let n = arr.length;
let k = 3;
printmissingk(arr, n, k);
</script>
|
Time complexity: O(n+k)
Auxiliary Space: O(n)
This article is contributed by Biswajit Mohapatra. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!