Maximize count of pairs ( i , j ) from two arrays having element from first array not exceeding that from second array | Function to return the maximum number of required pairs ; Max Heap to add values of arr2 [ ] ; Sort the array arr1 [ ] ; Push all arr2 [ ] into Max Heap ; Initialize the ans ; Traverse the arr1 [ ] in decreasing order ; Remove element until a required pair is found ; Return maximum number of pairs ; Driver Code ; Given arrays ; Function Call
def numberOfPairs ( arr1 , n , arr2 , m ) : NEW_LINE INDENT pq = [ ] NEW_LINE arr1 . sort ( reverse = False ) NEW_LINE for j in range ( m ) : NEW_LINE INDENT pq . append ( arr2 [ j ] ) NEW_LINE DEDENT ans = 2 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT pq . sort ( reverse = False ) NEW_LINE if ( pq [ 0 ] >= 2 * arr1 [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE print ( pq [ 0 ] ) NEW_LINE pq . remove ( pq [ 0 ] ) NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 3 , 2 , 1 ] NEW_LINE arr2 = [ 3 , 4 , 2 , 1 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE print ( numberOfPairs ( arr1 , N , arr2 , M ) ) NEW_LINE DEDENT