k | C # 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 . ; Insert all elements of givens sequence b [ ] . ; Traverse through increasing sequence and keep track of count of missing numbers . ; Driver code
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 ) ) ; } }