Given two sequences, one is increasing sequence a[] and another a normal sequence b[], find the K-th missing element in the increasing sequence which is not present in the given sequence. If no k-th missing element is there output -1
Examples:
Input: a[] = {0, 2, 4, 6, 8, 10, 12, 14, 15};
b[] = {4, 10, 6, 8, 12};
k = 3
Output: 14
Explanation : The numbers from increasing sequence that
are not present in the given sequence are 0, 2, 14, 15.
The 3rd missing number is 14.
n1 Number of elements on increasing sequence a[].
n2 Number of elements in given sequence b[].
A naive approach is to iterate for every element in the increasing sequence and check if it is present in the given sequence or not, and keep a counter of not present elements, and print the k-th non-present element. This will not be efficient enough as it has two nested for loops which will take O(n2).
Time complexity: O(n1 * n2)
Auxiliary space: O(1)
An efficient approach is to use hashing. We store all elements of given sequence in a hash table. Then we iterate through all elements of increasing sequence. For every element, we search it in the hash table. If element is present in not hash table, then we increment count of missing elements. If count becomes k, we return the missing element.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int find( int a[], int b[], int k, int n1, int n2)
{
unordered_set< int > s;
for ( int i = 0; i < n2; i++)
s.insert(b[i]);
int missing = 0;
for ( int i = 0; i < n1; i++) {
if (s.find(a[i]) == s.end())
missing++;
if (missing == k)
return a[i];
}
return -1;
}
int main()
{
int a[] = { 0, 2, 4, 6, 8, 10, 12, 14, 15 };
int b[] = { 4, 10, 6, 8, 12 };
int n1 = sizeof (a) / sizeof (a[0]);
int n2 = sizeof (b) / sizeof (b[0]);
int k = 3;
cout << find(a, b, k, n1, n2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int find( int a[], int b[], int k, int n1, int n2)
{
LinkedHashSet<Integer> s = new LinkedHashSet<>();
for ( int i = 0 ; i < n2; i++)
s.add(b[i]);
int missing = 0 ;
for ( int i = 0 ; i < n1; i++)
{
if (!s.contains(a[i]) )
missing++;
if (missing == k)
return a[i];
}
return - 1 ;
}
public static void main(String[] args)
{
int a[] = { 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 };
int b[] = { 4 , 10 , 6 , 8 , 12 };
int n1 = a.length;
int n2 = b.length;
int k = 3 ;
System.out.println(find(a, b, k, n1, n2));
}
}
|
Python3
def find(a, b, k, n1, n2):
s = set ()
for i in range (n2):
s.add(b[i])
missing = 0
for i in range (n1):
if a[i] not in s:
missing + = 1
if missing = = k:
return a[i]
return - 1
a = [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 ]
b = [ 4 , 10 , 6 , 8 , 12 ]
n1 = len (a)
n2 = len (b)
k = 3
print (find(a, b, k, n1, n2))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int find( int []a, int []b, int k, int n1, int n2)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n2; i++)
s.Add(b[i]);
int missing = 0;
for ( int i = 0; i < n1; i++)
{
if (!s.Contains(a[i]) )
missing++;
if (missing == k)
return a[i];
}
return -1;
}
public static void Main(String[] args)
{
int []a = { 0, 2, 4, 6, 8, 10, 12, 14, 15 };
int []b = { 4, 10, 6, 8, 12 };
int n1 = a.Length;
int n2 = b.Length;
int k = 3;
Console.WriteLine(find(a, b, k, n1, n2));
}
}
|
Javascript
<script>
function find(a, b, k, n1, n2)
{
var s = new Set();
for ( var i = 0; i < n2; i++)
s.add(b[i]);
var missing = 0;
for ( var i = 0; i < n1; i++) {
if (!s.has(a[i]))
missing++;
if (missing == k)
return a[i];
}
return -1;
}
var a = [0, 2, 4, 6, 8, 10, 12, 14, 15];
var b = [4, 10, 6, 8, 12];
var n1 = a.length;
var n2 = b.length;
var k = 3;
document.write( find(a, b, k, n1, n2));
</script>
|
Time complexity: O(n1 + n2)
Auxiliary Space: O(n2)
If you like GeeksforGeeks 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 GeeksforGeeks 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 geeks!
Last Updated :
18 Sep, 2023
Like Article
Save Article