k-th missing element in sorted array

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 : 4
Explanation: Missing Element in the increasing 
sequence are {4, 6, 7, 8}. So k-th missing element
is 4

Input : a[] = {2, 3, 5, 9, 10, 11, 12};       
        k = 4;
Output : 8
Explanation: missing element in the increasing 
sequence are {4, 6, 7, 8}  so k-th missing 
element is 8

Approach: Start iterating over the array elements, and for every element check if next element is consecutive or not, if not, then take difference between these two, and check if difference is greater than or equal to given k, then calculate ans = a[i] + count, else iterate for next element.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
// Function to find k-th 
// missing element
int missingK(int a[], int k, 
             int n)
{
    int difference = 0, 
        ans = 0, count = k;
    bool flag = 0;
      
    // interating over the array
    for(int i = 0 ; i < n - 1; i++)
    {   
        difference = 0;
          
        // check if i-th and 
        // (i + 1)-th element 
        // are not consecutive
        if ((a[i] + 1) != a[i + 1]) 
        {
              
            // save their difference
            difference += 
                (a[i + 1] - a[i]) - 1;
              
            // check for difference
            // and given k
            if (difference >= count)
                {
                    ans = a[i] + count;
                    flag = 1;
                    break;
                }
            else
                count -= difference;
        }
    }
      
    // if found
    if(flag)
        return ans;
    else
        return  -1;
}
  
// Driver code
int main()
{
    // Input array
    int a[] = {1, 5, 11, 19};
      
    // k-th missing element 
    // to be found in the array
    int k = 11;
    int n = sizeof(a) / sizeof(a[0]);
      
    // calling function to
    // find missing element
    int missing = missingK(a, k, n);
      
    cout << missing << endl;
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check for
// even or odd
import java.io.*;
import java.util.*;
   
public class GFG {
       
    // Function to find k-th 
    // missing element
    static int missingK(int []a, int k, 
                                 int n)
    {
        int difference = 0
            ans = 0, count = k;
        boolean flag = false;
           
        // interating over the array
        for(int i = 0 ; i < n - 1; i++)
        
            difference = 0;
               
            // check if i-th and 
            // (i + 1)-th element 
            // are not consecutive
            if ((a[i] + 1) != a[i + 1]) 
            {
                   
                // save their difference
                difference += 
                    (a[i + 1] - a[i]) - 1;
                   
                // check for difference
                // and given k
                if (difference >= count)
                    {
                        ans = a[i] + count;
                        flag = true;
                        break;
                    }
                else
                    count -= difference;
            }
        }
           
        // if found
        if(flag)
            return ans;
        else
            return -1;
    }
       
    // Driver code
    public static void main(String args[])
    {
           
        // Input array
        int []a = {1, 5, 11, 19};
           
        // k-th missing element 
        // to be found in the array
        int k = 11;
        int n = a.length;
           
        // calling function to
        // find missing element
        int missing = missingK(a, k, n);
           
        System.out.print(missing);
    }
   
}
   
// This code is contributed by
// Manish Shaw (manishshaw1)

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to find k-th 
# missing element
def missingK(a, k, n) :
  
    difference = 0
    ans = 0
    count = k
    flag = 0
      
    # interating over the array
    for i in range (0, n-1) : 
        difference = 0
          
        # check if i-th and 
        # (i + 1)-th element 
        # are not consecutive
        if ((a[i] + 1) != a[i + 1]) :
          
              
            # save their difference
            difference += (a[i + 1] - a[i]) - 1
              
            # check for difference
            # and given k
            if (difference >= count) :
                    ans = a[i] + count
                    flag = 1
                    break
            else :
                count -= difference     
      
    # if found
    if(flag) :
        return ans
    else :
        return -1
  
# Driver code
# Input array
a = [1, 5, 11, 19]
  
# k-th missing element 
# to be found in the array
k = 11
n = len(a)
  
# calling function to
# find missing element
missing = missingK(a, k, n)
  
print(missing)
  
# This code is contributed by 
# Manish Shaw (manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check for
// even or odd
using System;
using System.Collections.Generic;
  
class GFG {
      
    // Function to find k-th 
    // missing element
    static int missingK(int []a, int k, 
                                 int n)
    {
        int difference = 0, 
            ans = 0, count = k;
        bool flag = false;
          
        // interating over the array
        for(int i = 0 ; i < n - 1; i++)
        
            difference = 0;
              
            // check if i-th and 
            // (i + 1)-th element 
            // are not consecutive
            if ((a[i] + 1) != a[i + 1]) 
            {
                  
                // save their difference
                difference += 
                    (a[i + 1] - a[i]) - 1;
                  
                // check for difference
                // and given k
                if (difference >= count)
                    {
                        ans = a[i] + count;
                        flag = true;
                        break;
                    }
                else
                    count -= difference;
            }
        }
          
        // if found
        if(flag)
            return ans;
        else
            return -1;
    }
      
    // Driver code
    public static void Main()
    {
          
        // Input array
        int []a = {1, 5, 11, 19};
          
        // k-th missing element 
        // to be found in the array
        int k = 11;
        int n = a.Length;
          
        // calling function to
        // find missing element
        int missing = missingK(a, k, n);
          
        Console.Write(missing);
    }
  
}
  
// This code is contributed by
// Manish Shaw (manishshaw1)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Function to find k-th 
// missing element
function missingK(&$a, $k, $n)
{
    $difference = 0;
    $ans = 0;
    $count = $k;
    $flag = 0;
      
    // interating over the array
    for($i = 0 ; $i < $n - 1; $i++)
    
        $difference = 0;
          
        // check if i-th and 
        // (i + 1)-th element 
        // are not consecutive
        if (($a[$i] + 1) != $a[$i + 1]) 
        {
              
            // save their difference
            $difference += ($a[$i + 1] - 
                            $a[$i]) - 1;
              
            // check for difference
            // and given k
            if ($difference >= $count)
                {
                    $ans = $a[$i] + $count;
                    $flag = 1;
                    break;
                }
            else
                $count -= $difference;
        }
    }
      
    // if found
    if($flag)
        return $ans;
    else
        return -1;
}
  
// Driver Code
  
// Input array
$a = array(1, 5, 11, 19);
  
// k-th missing element 
// to be found in the array
$k = 11;
$n = count($a);
  
// calling function to
// find missing element
$missing = missingK($a, $k, $n);
  
echo $missing;
  
// This code is contributed by Manish Shaw
// (manishshaw1)
?>

chevron_right


Output :

14

Time Complexity :O(n), where n is the number of elements in the array.



My Personal Notes arrow_drop_up

Sometimes you never realize the value of a moment until it becomes a memory

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : manishshaw1



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.