k-th missing element in an unsorted array
Given an unsorted sequence a[], the task is to find the K-th missing contiguous element in the increasing sequence of the array elements i.e. consider the array in sorted order and find the kth missing number. If no k-th missing element is there output -1.
Note: Only elements exists in the range of minimum and maximum element to be considered.
Examples:
Input: arr[] = {2, 4, 10, 7}, k = 5
Output: 9
Missing elements in the given array: 3, 5, 6, 8, 9
5th missing is 9.
Input: arr[] = {1, 3, 4}, k = 5
Output: -1
Method-1: Sort the array and use the approach used in the k-th missing element in a sorted array.
Method-2:
- Insert all the elements in an unordered_set.
- Find the minimum and maximum element of the array.
- Traverse the elements from minimum to maximum.
- Check if current element is present in the set or not.
- If not then check if this is kth missing by counting the missing elements.
- Return the current element if this is current missing.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findKth( int arr[], int n, int k)
{
unordered_set< int > missing;
int count = 0;
for ( int i = 0; i < n; i++)
missing.insert(arr[i]);
int maxm = *max_element(arr, arr + n);
int minm = *min_element(arr, arr + n);
for ( int i = minm + 1; i < maxm; i++) {
if (missing.find(i) == missing.end())
count++;
if (count == k)
return i;
}
return -1;
}
int main()
{
int arr[] = { 2, 10, 9, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 5;
cout << findKth(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findKth( int arr[], int n, int k)
{
HashSet<Integer> missing = new HashSet<>();
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
missing.add(arr[i]);
}
int maxm = Arrays.stream(arr).max().getAsInt();
int minm = Arrays.stream(arr).min().getAsInt();
for ( int i = minm+ 1 ; i < maxm; i++)
{
if (!missing.contains(i))
{
count++;
}
if (count == k)
{
return i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 10 , 9 , 4 };
int n = arr.length;
int k = 5 ;
System.out.println(findKth(arr, n, k));
}
}
|
Python
def findKth( arr, n, k):
missing = dict ()
count = 0
for i in range (n):
missing[arr[i]] = 1
maxm = max (arr)
minm = min (arr)
for i in range (minm + 1 , maxm):
if (i not in missing.keys()):
count + = 1
if (count = = k):
return i
return - 1
arr = [ 2 , 10 , 9 , 4 ]
n = len (arr)
k = 5
print (findKth(arr, n, k))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int findKth( int []arr, int n, int k)
{
HashSet< int > missing = new HashSet< int >();
int count = 0;
for ( int i = 0; i < n; i++)
{
missing.Add(arr[i]);
}
int maxm = arr.Max();
int minm = arr.Min();
for ( int i = minm + 1; i < maxm; i++)
{
if (!missing.Contains(i))
{
count++;
}
if (count == k)
{
return i;
}
}
return -1;
}
public static void Main(String[] args)
{
int []arr = {2, 10, 9, 4};
int n = arr.Length;
int k = 5;
Console.WriteLine(findKth(arr, n, k));
}
}
|
PHP
<?php
function findKth( $arr , $n , $k )
{
$missing = array ();
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
array_push ( $missing , $arr [ $i ]);
$missing = array_unique ( $missing );
$maxm = max( $arr );
$minm = min( $arr );
for ( $i = $minm + 1; $i < $maxm ; $i ++)
{
if (!in_array( $i , $missing , false))
$count += 1;
if ( $count == $k )
return $i ;
}
return -1;
}
$arr = array (2, 10, 9, 4);
$n = sizeof( $arr );
$k = 5;
echo findKth( $arr , $n , $k );
?>
|
Javascript
<script>
function findKth(arr, n, k)
{
var missing = new Set();
var count = 0;
for ( var i = 0; i < n; i++)
missing.add(arr[i]);
var maxm = arr.reduce((a,b)=>Math.max(a,b));
var minm = arr.reduce((a,b)=>Math.min(a,b));
for ( var i = minm + 1; i < maxm; i++) {
if (!missing.has(i))
count++;
if (count == k)
return i;
}
return -1;
}
var arr = [ 2, 10, 9, 4 ];
var n = arr.length;
var k = 5;
document.write( findKth(arr, n, k));
</script>
|
Time complexity: O(n) where n is size of input array
Auxiliary Space: O(n)
Last Updated :
11 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...