K | Java 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
import java . util . * ; class GFG { static int kth ( int a [ ] , int b [ ] , int n , int m , int k ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . offer ( a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { pq . offer ( b [ i ] ) ; } while ( k -- > 1 ) { pq . remove ( ) ; } return pq . peek ( ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . out . print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }