Given an increasing sequence a[], we need to find the K-th missing contiguous element in the increasing sequence which is not present in the sequence. If no k-th missing element is there output -1.
Examples :
Input : a[] = {2, 3, 5, 9, 10};
k = 1;
Output : 1
Explanation: Missing Element in the increasing
sequence are {1,4, 6, 7, 8}. So k-th missing element
is 1
Input : a[] = {2, 3, 5, 9, 10, 11, 12};
k = 4;
Output : 7
Explanation: missing element in the increasing
sequence are {1, 4, 6, 7, 8} so k-th missing
element is 7
Approach 1: Start iterating over the array elements, and for every element check if the next element is consecutive or not, if not, then take the difference between these two, and check if the difference is greater than or equal to given k, then calculate ans = a[i] + count, else iterate for next element.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int missingK( int a[], int k, int n)
{
int difference = 0, ans = 0, count = k;
bool flag = 0;
if (a[0] != 1) {
difference = a[0] - 1;
if (difference >= count)
return count;
count -= difference;
}
for ( int i = 0; i < n - 1; i++) {
difference = 0;
if ((a[i] + 1) != a[i + 1]) {
difference += (a[i + 1] - a[i]) - 1;
if (difference >= count) {
ans = a[i] + count;
flag = 1;
break ;
}
else
count -= difference;
}
}
if (flag)
return ans;
else
return -1;
}
int main()
{
int a[] = { 1, 5, 11, 19 };
int k = 11;
int n = sizeof (a) / sizeof (a[0]);
int missing = missingK(a, k, n);
cout << missing << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static int missingK( int [] a, int k, int n)
{
int difference = 0 , ans = 0 , count = k;
boolean flag = false ;
if (a[ 0 ] != 1 ) {
difference = a[ 0 ] - 1 ;
if (difference >= count)
return count;
count -= difference;
}
for ( int i = 0 ; i < n - 1 ; i++) {
difference = 0 ;
if ((a[i] + 1 ) != a[i + 1 ]) {
difference += (a[i + 1 ] - a[i]) - 1 ;
if (difference >= count) {
ans = a[i] + count;
flag = true ;
break ;
}
else
count -= difference;
}
}
if (flag)
return ans;
else
return - 1 ;
}
public static void main(String args[])
{
int [] a = { 1 , 5 , 11 , 19 };
int k = 11 ;
int n = a.length;
int missing = missingK(a, k, n);
System.out.print(missing);
}
}
|
Python3
def missingK(a, k, n):
difference = 0
ans = 0
count = k
flag = 0
if a[ 0 ] ! = 1 :
difference = a[ 0 ] - 1
if difference > = count:
return count
count - = difference
for i in range ( 0 , n - 1 ):
difference = 0
if ((a[i] + 1 ) ! = a[i + 1 ]):
difference + = (a[i + 1 ] - a[i]) - 1
if (difference > = count):
ans = a[i] + count
flag = 1
break
else :
count - = difference
if (flag):
return ans
else :
return - 1
a = [ 1 , 5 , 11 , 19 ]
k = 11
n = len (a)
missing = missingK(a, k, n)
print (missing)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int missingK( int [] a, int k, int n)
{
int difference = 0, ans = 0, count = k;
bool flag = false ;
if (a[0] != 1) {
difference = a[0] - 1;
if (difference >= count)
return count;
count -= difference;
}
for ( int i = 0; i < n - 1; i++) {
difference = 0;
if ((a[i] + 1) != a[i + 1]) {
difference += (a[i + 1] - a[i]) - 1;
if (difference >= count) {
ans = a[i] + count;
flag = true ;
break ;
}
else
count -= difference;
}
}
if (flag)
return ans;
else
return -1;
}
public static void Main()
{
int [] a = { 1, 5, 11, 19 };
int k = 11;
int n = a.Length;
int missing = missingK(a, k, n);
Console.Write(missing);
}
}
|
PHP
<?php
function missingK(& $a , $k , $n )
{
$difference = 0;
$ans = 0;
$count = $k ;
$flag = 0;
for ( $i = 0 ; $i < $n - 1; $i ++)
{
$difference = 0;
if (( $a [ $i ] + 1) != $a [ $i + 1])
{
$difference += ( $a [ $i + 1] -
$a [ $i ]) - 1;
if ( $difference >= $count )
{
$ans = $a [ $i ] + $count ;
$flag = 1;
break ;
}
else
$count -= $difference ;
}
}
if ( $flag )
return $ans ;
else
return -1;
}
$a = array (1, 5, 11, 19);
$k = 11;
$n = count ( $a );
$missing = missingK( $a , $k , $n );
echo $missing ;
?>
|
Javascript
<script>
function missingK(a, k, n)
{
let difference = 0, ans = 0, count = k;
let flag = false ;
if (a[0] != 1){
difference = a[0]-1;
if (difference >= count){
return count;
}
count -= difference;
}
for (let i = 0 ; i < n - 1; i++)
{
difference = 0;
if ((a[i] + 1) != a[i + 1])
{
difference += (a[i + 1] - a[i]) - 1;
if (difference >= count)
{
ans = a[i] + count;
flag = true ;
break ;
}
else
count -= difference;
}
}
if (flag)
return ans;
else
return -1;
}
let a = [ 1, 5, 11, 19 ];
let k = 11;
let n = a.length;
let missing = missingK(a, k, n);
document.write(missing);
</script>
|
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Approach 2:
Apply a binary search. Since the array is sorted we can find at any given index how many numbers are missing as arr[index] – (index+1). We would leverage this knowledge and apply binary search to narrow down our hunt to find that index from which getting the missing number is easier.
Implementation:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int missingK(vector< int >& arr, int k)
{
int n = arr.size();
int l = 0, u = n - 1, mid;
while (l <= u) {
mid = (l + u) / 2;
int numbers_less_than_mid = arr[mid] - (mid + 1);
if (numbers_less_than_mid == k) {
if (mid > 0 && (arr[mid - 1] - (mid)) == k) {
u = mid - 1;
continue ;
}
return arr[mid] - 1;
}
if (numbers_less_than_mid < k) {
l = mid + 1;
}
else if (k < numbers_less_than_mid) {
u = mid - 1;
}
}
if (u < 0)
return k;
int less = arr[u] - (u + 1);
k -= less;
return arr[u] + k;
}
int main()
{
vector< int > arr = { 2, 3, 4, 7, 11 };
int k = 5;
cout << "Missing kth number = " << missingK(arr, k)
<< endl;
return 0;
}
|
Java
public class GFG {
static int missingK( int [] arr, int k)
{
int n = arr.length;
int l = 0 , u = n - 1 , mid;
while (l <= u) {
mid = (l + u) / 2 ;
int numbers_less_than_mid
= arr[mid] - (mid + 1 );
if (numbers_less_than_mid == k) {
if (mid > 0
&& (arr[mid - 1 ] - (mid)) == k) {
u = mid - 1 ;
continue ;
}
return arr[mid] - 1 ;
}
if (numbers_less_than_mid < k) {
l = mid + 1 ;
}
else if (k < numbers_less_than_mid) {
u = mid - 1 ;
}
}
if (u < 0 )
return k;
int less = arr[u] - (u + 1 );
k -= less;
return arr[u] + k;
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 4 , 7 , 11 };
int k = 5 ;
System.out.println( "Missing kth number = "
+ missingK(arr, k));
}
}
|
Python3
def missingK(arr, k):
n = len (arr)
l = 0
u = n - 1
mid = 0
while (l < = u):
mid = (l + u) / / 2
numbers_less_than_mid = arr[mid] - (mid + 1 )
if (numbers_less_than_mid = = k):
if (mid > 0 and (arr[mid - 1 ] - (mid)) = = k):
u = mid - 1
continue
return arr[mid] - 1
if (numbers_less_than_mid < k):
l = mid + 1
elif (k < numbers_less_than_mid):
u = mid - 1
if (u < 0 ):
return k
less = arr[u] - (u + 1 )
k - = less
return arr[u] + k
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 7 , 11 ]
k = 5
print ( "Missing kth number = " + str (missingK(arr, k)))
|
C#
using System;
class GFG {
static int missingK( int [] arr, int k)
{
int n = arr.Length;
int l = 0, u = n - 1, mid;
while (l <= u) {
mid = (l + u) / 2;
int numbers_less_than_mid
= arr[mid] - (mid + 1);
if (numbers_less_than_mid == k) {
if (mid > 0
&& (arr[mid - 1] - (mid)) == k) {
u = mid - 1;
continue ;
}
return arr[mid] - 1;
}
if (numbers_less_than_mid < k) {
l = mid + 1;
}
else if (k < numbers_less_than_mid) {
u = mid - 1;
}
}
if (u < 0)
return k;
int less = arr[u] - (u + 1);
k -= less;
return arr[u] + k;
}
static void Main()
{
int [] arr = { 2, 3, 4, 7, 11 };
int k = 5;
Console.WriteLine( "Missing kth number = "
+ missingK(arr, k));
}
}
|
Javascript
<script>
function missingK(arr, k)
{
var n = arr.length;
var l = 0, u = n - 1, mid;
while (l <= u)
{
mid = (l + u)/2;
var numbers_less_than_mid = arr[mid] -
(mid + 1);
if (numbers_less_than_mid == k)
{
if (mid > 0 && (arr[mid - 1] - (mid)) == k)
{
u = mid - 1;
continue ;
}
return arr[mid] - 1;
}
if (numbers_less_than_mid < k)
{
l = mid + 1;
}
else if (k < numbers_less_than_mid)
{
u = mid - 1;
}
}
if (u < 0)
return k;
var less = arr[u] - (u + 1);
k -= less;
return arr[u] + k;
}
var arr = [2,3,4,7,11];
var k = 5;
document.write( "Missing kth number = " + missingK(arr, k));
</script>
|
Output
Missing kth number = 9
Time Complexity: O(logn), where n is the number of elements in the array.
Auxiliary Space: O(1)
Approach 3 (Using Map):
We will traverse the array and mark each of the elements as visited in the map and we will also keep track of the min and max element present so that we know the lower and upper bound for the given particular input. Then we start a loop from lower to upper bound and maintain a count variable. As soon we found an element that is not present in the map we increment the count and until the count becomes equal to k.
Implementation:
C++14
#include <iostream>
#include <unordered_map>
using namespace std;
int solve( int arr[], int k, int n)
{
unordered_map< int , int > umap;
int mins = 99999;
int maxs = -99999;
for ( int i = 0; i < n; i++) {
umap[arr[i]]
= 1;
if (mins > arr[i])
mins = arr[i];
if (maxs < arr[i])
maxs = arr[i];
}
int counts = 0;
for ( int i = mins; i <= maxs; i++) {
if (umap[i] == 0)
counts++;
if (counts == k)
return i;
}
return -1;
}
int main()
{
int arr[] = { 2, 3, 5, 9, 10, 11, 12 };
int k = 4;
cout << solve(arr, k, 7);
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
public static int solve( int arr[], int k, int n)
{
HashMap<Integer, Integer> umap
= new HashMap<Integer, Integer>();
int mins = 99999 ;
int maxs = - 99999 ;
for ( int i = 0 ; i < n; i++) {
umap.put(
arr[i],
1 );
if (mins > arr[i])
mins = arr[i];
if (maxs < arr[i])
maxs = arr[i];
}
int counts = 0 ;
for ( int i = mins; i <= maxs; i++) {
if (umap.get(i) == null )
counts++;
if (counts == k)
return i;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 5 , 9 , 10 , 11 , 12 };
int k = 4 ;
System.out.println(
solve(arr, k, 7 ));
}
}
|
Python3
def solve(arr, k, n):
umap = {}
mins = 99999
maxs = - 99999
for i in range (n):
umap[arr[i]] = 1
if (mins > arr[i]):
mins = arr[i]
if (maxs < arr[i]):
maxs = arr[i]
counts = 0
for i in range (mins, maxs + 1 ):
if (i not in umap):
counts + = 1
if (counts = = k):
return i
return - 1
arr = [ 2 , 3 , 5 , 9 , 10 , 11 , 12 ]
k = 4
print (solve(arr, k, 7 ))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int solve( int [] arr, int k, int n)
{
Dictionary< int , int > umap
= new Dictionary< int , int >();
int mins = 99999;
int maxs = -99999;
for ( int i = 0; i < n; i++) {
umap.Add(
arr[i],
1);
if (mins > arr[i])
mins = arr[i];
if (maxs < arr[i])
maxs = arr[i];
}
int counts = 0;
for ( int i = mins; i <= maxs; i++) {
if (!umap.ContainsKey(i))
counts++;
if (counts == k)
return i;
}
return -1;
}
static void Main()
{
int [] arr = { 2, 3, 5, 9, 10, 11, 12 };
int k = 4;
int n = arr.Length;
Console.WriteLine( "Missing kth number = "
+ solve(arr, k, n));
}
}
|
Javascript
<script>
function solve(arr ,k ,n){
let umap = new Map()
let mins = 99999
let maxs = -99999
for (let i = 0; i < n; i++){
umap.set(arr[i] , 1)
if (mins > arr[i])
mins = arr[i]
if (maxs < arr[i])
maxs = arr[i]
}
let counts = 0
for (let i = mins; i < maxs + 1; i++){
if (!umap.has(i))
counts += 1
if (counts == k)
return i
}
return -1
}
let arr = [2, 3, 5, 9, 10, 11, 12]
let k = 4
document.write(solve(arr , k , 7), "</br>" )
</script>
|
Time Complexity: O(n+m), where n is the number of elements in the array and m is the difference between the largest and smallest element of the array.
Auxiliary Space: O(n)
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 geeks!
Last Updated :
16 Oct, 2022
Like Article
Save Article