K | C ++ Program to find kth element from two sorted arrays ; Function to find K - th min ; Declaring a min heap ; Pushing elements for array a to min - heap ; Pushing elements for array b to min - heap ; Poping - out K - 1 elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kth ( int * a , int * b , int n , int m , int k ) { priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 0 ; i < n ; i ++ ) { pq . push ( a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { pq . push ( b [ i ] ) ; } while ( k -- > 1 ) { pq . pop ( ) ; } return pq . top ( ) ; } int main ( ) { int arr1 [ 5 ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ 4 ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; cout << kth ( arr1 , arr2 , 5 , 4 , k ) ; return 0 ; }