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++

`// C++ program to find the k-th missing element ` `// in a given sequence ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns k-th missing element. It returns -1 if ` `// no k is more than number of missing elements. ` `int` `find(` `int` `a[], ` `int` `b[], ` `int` `k, ` `int` `n1, ` `int` `n2) ` `{ ` ` ` `// Insert all elements of givens sequence b[]. ` ` ` `unordered_set<` `int` `> s; ` ` ` `for` `(` `int` `i = 0; i < n2; i++) ` ` ` `s.insert(b[i]); ` ` ` ` ` `// Traverse through increasing sequence and ` ` ` `// keep track of count of missing numbers. ` ` ` `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; ` `} ` ` ` `// driver program to test the above function ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the k-th missing element ` `// in a given sequence ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns k-th missing element. It returns -1 if ` `// no k is more than number of missing elements. ` `static` `int` `find(` `int` `a[], ` `int` `b[], ` `int` `k, ` `int` `n1, ` `int` `n2) ` `{ ` ` ` `// Insert all elements of givens sequence b[]. ` ` ` `LinkedHashSet<Integer> s = ` `new` `LinkedHashSet<>(); ` ` ` `for` `(` `int` `i = ` `0` `; i < n2; i++) ` ` ` `s.add(b[i]); ` ` ` ` ` `// Traverse through increasing sequence and ` ` ` `// keep track of count of missing numbers. ` ` ` `int` `missing = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n1; i++) ` ` ` `{ ` ` ` `if` `(!s.contains(a[i]) ) ` ` ` `missing++; ` ` ` `if` `(missing == k) ` ` ` `return` `a[i]; ` ` ` `} ` ` ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` `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)); ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the k-th ` `# missing element in a given sequence ` ` ` ` ` `# Returns k-th missing element. It returns -1 if ` `# no k is more than number of missing elements. ` `def` `find(a, b, k, n1, n2): ` ` ` ` ` `# insert all elements of ` ` ` `# given sequence b[]. ` ` ` `s ` `=` `set` `() ` ` ` ` ` `for` `i ` `in` `range` `(n2): ` ` ` `s.add(b[i]) ` ` ` ` ` `# Traverse through increasing sequence and ` ` ` `# keep track of count of missing numbers. ` ` ` `missing ` `=` `0` ` ` `for` `i ` `in` `range` `(n1): ` ` ` `if` `a[i] ` `not` `in` `s: ` ` ` `missing ` `+` `=` `1` ` ` `if` `missing ` `=` `=` `k: ` ` ` `return` `a[i] ` ` ` `return` `-` `1` ` ` `# Driver code ` `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)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the k-th missing element ` `// in a given sequence ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns k-th missing element. It returns -1 if ` `// no k is more than number of missing elements. ` `static` `int` `find(` `int` `[]a, ` `int` `[]b, ` `int` `k, ` `int` `n1, ` `int` `n2) ` `{ ` ` ` `// Insert all elements of givens sequence b[]. ` ` ` `HashSet<` `int` `> s = ` `new` `HashSet<` `int` `>(); ` ` ` `for` `(` `int` `i = 0; i < n2; i++) ` ` ` `s.Add(b[i]); ` ` ` ` ` `// Traverse through increasing sequence and ` ` ` `// keep track of count of missing numbers. ` ` ` `int` `missing = 0; ` ` ` `for` `(` `int` `i = 0; i < n1; i++) ` ` ` `{ ` ` ` `if` `(!s.Contains(a[i]) ) ` ` ` `missing++; ` ` ` `if` `(missing == k) ` ` ` `return` `a[i]; ` ` ` `} ` ` ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `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)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

14

**Time complexity:** O(n1 + n2)

**Auxiliary Space:** O(n2)

This article is contributed by **Raja Vikramaditya**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Find elements which are present in first array and not in second
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Swap Kth node from beginning with Kth node from end in a Linked List
- Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
- Smallest element greater than X not present in the array
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Find the maximum element in an array which is first increasing and then decreasing
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Check if all array elements are present in a given stack or not
- Reduce array to a single element by repeatedly removing an element from any increasing pair
- Find missing number in another array which is shuffled copy
- Count of Subsets of a given Set with element X present in it
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count elements present in first array but not in second
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Find the kth element in the series generated by the given N ranges
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- k-th missing element in an unsorted array