Count of pairs from arrays A and B such that element in A is greater than element in B at that index | Function to find the maximum count of values that follow the given condition ; Initializing the max - heap for the array A [ ] ; Adding the values of A [ ] into max heap ; Adding the values of B [ ] into max heap ; Counter variable ; Loop to iterate through the heap ; Comparing the values at the top . If the value of heap A [ ] is greater , then counter is incremented ; Driver code
function check ( A , B , N ) { let pq1 = [ ] ; let pq2 = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { pq1 . push ( A [ i ] ) ; } for ( let i = 0 ; i < N ; i ++ ) { pq2 . push ( B [ i ] ) ; } pq1 . sort ( function ( a , b ) { return a - b ; } ) ; pq1 . reverse ( ) ; pq2 . sort ( function ( a , b ) { return a - b ; } ) ; pq2 . reverse ( ) ; let c = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( pq1 [ 0 ] > pq2 [ 0 ] ) { c ++ ; pq1 . shift ( ) ; pq2 . shift ( ) ; } else { if ( pq2 . length == 0 ) { break ; } pq2 . shift ( ) ; } } return ( c ) ; } let A = [ 10 , 3 , 7 , 5 , 8 ] ; let B = [ 8 , 6 , 2 , 5 , 9 ] ; let N = A . length ; document . write ( check ( A , B , N ) ) ;