Count of pairs from arrays A and B such that element in A is greater than element in B at that index | Java 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
import java . util . * ; public class GFG { static int check ( int A [ ] , int B [ ] , int N ) { PriorityQueue < Integer > pq1 = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; PriorityQueue < Integer > pq2 = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { pq1 . add ( A [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { pq2 . add ( B [ i ] ) ; } int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pq1 . peek ( ) . compareTo ( pq2 . peek ( ) ) == 1 ) { c ++ ; pq1 . poll ( ) ; pq2 . poll ( ) ; } else { if ( pq2 . size ( ) == 0 ) { break ; } pq2 . poll ( ) ; } } return ( c ) ; } public static void main ( String args [ ] ) { int A [ ] = { 10 , 3 , 7 , 5 , 8 } ; int B [ ] = { 8 , 6 , 2 , 5 , 9 } ; int N = A . length ; System . out . println ( check ( A , B , N ) ) ; } }