k | C # implementation of the above approach ; Function to find the sum of minimum of all subarrays ; Insert all the elements in a set ; Find the maximum and minimum element ; Traverse from the minimum to maximum element ; Check if " i " is missing ; Check if it is kth missing ; If no kth element is missing ; Driver code
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 ) ) ; } }