Find k | C # implementation to solve k queries for given n ranges ; Structure to store the start and end point ; Comparison function for sorting ; Function to find Kth smallest number in a Vector of merged intervals ; Traverse merged . get ( ) o find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged . get ( . ) if ( curr . s > prev . e ) ; Driver code ; Merge all intervals into merged . get ( ) ) ; Processing all queries on merged intervals
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public class Interval { public int s ; public int e ; public Interval ( int a , int b ) { s = a ; e = b ; } } ; class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { Interval first = ( Interval ) a ; Interval second = ( Interval ) b ; return first . s - second . s ; } } static int kthSmallestNum ( ArrayList merged , int k ) { int n = merged . Count ; for ( int j = 0 ; j < n ; j ++ ) { if ( k <= Math . Abs ( ( ( Interval ) merged [ j ] ) . e - ( ( Interval ) merged [ j ] ) . s + 1 ) ) return ( ( ( Interval ) merged [ j ] ) . s + k - 1 ) ; k = k - Math . Abs ( ( ( Interval ) merged [ j ] ) . e - ( ( Interval ) merged [ j ] ) . s + 1 ) ; } if ( k != 0 ) return - 1 ; return 0 ; } static ArrayList mergeIntervals ( ArrayList merged , Interval [ ] arr , int n ) { Array . Sort ( arr , new sortHelper ( ) ) ; merged . Add ( ( Interval ) arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { Interval prev = ( Interval ) merged [ merged . Count - 1 ] ; Interval curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) { ( ( Interval ) merged [ merged . Count - 1 ] ) . e = ( ( Interval ) curr ) . e ; } else { merged . Add ( curr ) ; } } return merged ; } public static void Main ( string [ ] args ) { Interval [ ] arr = { new Interval ( 2 , 6 ) , new Interval ( 4 , 7 ) } ; int n = arr . Length ; int [ ] query = { 5 , 8 } ; int q = query . Length ; ArrayList merged = new ArrayList ( ) ; merged = mergeIntervals ( merged , arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( kthSmallestNum ( merged , query [ i ] ) ) ; } }