Kth smallest element from an array of intervals | 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
def KthSmallestNum ( arr , n , k ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT pq . append ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ) NEW_LINE DEDENT cnt = 1 NEW_LINE while ( cnt < k ) : NEW_LINE INDENT pq . sort ( reverse = True ) NEW_LINE interval = pq [ 0 ] NEW_LINE pq . remove ( pq [ 0 ] ) NEW_LINE if ( interval [ 0 ] < interval [ 1 ] ) : NEW_LINE INDENT pq . append ( [ interval [ 0 ] + 1 , interval [ 1 ] ] ) NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT pq . sort ( reverse = True ) NEW_LINE return pq [ 0 ] [ 0 ] + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 5 , 11 ] , [ 10 , 15 ] , [ 12 , 20 ] ] NEW_LINE n = len ( arr ) NEW_LINE k = 12 NEW_LINE print ( KthSmallestNum ( arr , n , k ) ) NEW_LINE DEDENT