Kth smallest element from an array of intervals | C # Program to implement the above approach ; Function to get the Kth smallest element from an array of intervals ; Store all the intervals so that it returns the minimum element in O ( 1 ) ; Insert all Intervals into the MinHeap ; Stores the count of popped elements ; Iterate over MinHeap ; Stores minimum element from all remaining intervals ; Remove minimum element ; Check if the minimum of the current interval is less than the maximum of the current interval ; Insert new interval ; Driver code ; Intervals given ; Size of the arr
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int KthSmallestNum ( int [ , ] arr , int n , int k ) { ArrayList pq = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . Add ( new Tuple < int , int > ( arr [ i , 0 ] , arr [ i , 1 ] ) ) ; } int cnt = 1 ; while ( cnt < k ) { pq . Sort ( ) ; pq . Reverse ( ) ; Tuple < int , int > interval = ( Tuple < int , int > ) pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( interval . Item1 < interval . Item2 ) { pq . Add ( new Tuple < int , int > ( interval . Item1 + 1 , interval . Item2 ) ) ; } cnt += 1 ; } pq . Sort ( ) ; pq . Reverse ( ) ; return ( ( Tuple < int , int > ) pq [ 0 ] ) . Item1 + 1 ; } static void Main ( ) { int [ , ] arr = { { 5 , 11 } , { 10 , 15 } , { 12 , 20 } } ; int n = arr . GetLength ( 0 ) ; int k = 12 ; Console . WriteLine ( KthSmallestNum ( arr , n , k ) ) ; } }