Count of pairs from arrays A and B such that element in A is greater than element in B at that index | C ++ program to find the maximum count of values that follow the given condition ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( int A [ ] , int B [ ] , int N ) { priority_queue < int > pq1 , pq2 ; for ( int i = 0 ; i < N ; i ++ ) { pq1 . push ( A [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { pq2 . push ( B [ i ] ) ; } int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pq1 . top ( ) > pq2 . top ( ) ) { c ++ ; pq1 . pop ( ) ; pq2 . pop ( ) ; } else { if ( pq2 . size ( ) == 0 ) { break ; } pq2 . pop ( ) ; } } return ( c ) ; } int main ( ) { int A [ ] = { 10 , 3 , 7 , 5 , 8 } ; int B [ ] = { 8 , 6 , 2 , 5 , 9 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << ( check ( A , B , N ) ) ; }