Given two arrays **A[]** and **B[]** of size **N**, the task is to count the maximum number of pairs, where each pair contains one from each array, such that **A[i] > B[i]**. Also the array A can be rearranged any number of times.

**Examples:**

Input:A[] = {20, 30, 50}, B[]= {60, 40, 25}

Output:2

Explanation:

Initially:

A[0] = 20 < B[0] = 60

A[1] = 30 < B[1] = 40

A[2] = 50 > B[2] = 25

Clearly, this arrangement has only 1 value such that A[i] > B[i].

This array A[] when rearranged to {20, 50, 30}:

A[0] = 20 < B[0] = 60

A[1] = 50 > B[1] = 40

A[2] = 30 > B[2] = 25

2 values follow the condition A[i] > B[i] which is the maximum for these set of arrays.

Input:A[] = {10, 3, 7, 5, 8}, B[] = {8, 6, 2, 5, 9}

Output:4

Explanation:

Initially:

A[0] = 10 > B[0] = 8

A[1] = 3 < B[1] = 6

A[2] = 7 > B[2] = 2

A[3] = 5 = B[3] = 5

A[4] = 8 < B[4] = 9

Clearly, this arrangement has only 2 values such that A[i] > B[i].

This array A[] when rearranged to {10, 8, 5, 7, 3}:

A[0] = 10 > B[0] = 8

A[1] = 8 > B[1] = 6

A[2] = 5 > B[2] = 2

A[3] = 7 > B[3] = 5

A[4] = 3 < B[4] = 9

4 values follow the condition A[i] > B[i] which is the maximum for these set of arrays.

**Approach:** The idea is to use the concept of heap. Since the arrangement of B[] doesn’t matter in the question, we can perform max heap on both the arrays. After performing max heap and storing the values in two different heaps, iterate through the heap corresponding to A[] and B[] to count the number of indices satisfying the given condition **A[i] > B[i]**.

Below is the implementation of the above approach:

## C++

`// CPP program to find the maximum count of ` `// values that follow the given condition ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum count of ` `// values that follow the given condition ` `int` `check(` `int` `A[], ` `int` `B[], ` `int` `N) ` `{ ` ` ` ` ` `// Initializing the max-heap for the array A[] ` ` ` `priority_queue <` `int` `> pq1,pq2; ` ` ` ` ` `// Adding the values of A[] into max heap ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `pq1.push(A[i]); ` ` ` `} ` ` ` ` ` `// Adding the values of B[] into max heap ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `pq2.push(B[i]); ` ` ` `} ` ` ` ` ` `// Counter variable ` ` ` `int` `c = 0; ` ` ` ` ` `// Loop to iterate through the heap ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `// Comparing the values at the top. ` ` ` `// If the value of heap A[] is greater, ` ` ` `// then counter is incremented ` ` ` `if` `(pq1.top()>pq2.top()) { ` ` ` `c++; ` ` ` `pq1.pop(); ` ` ` `pq2.pop(); ` ` ` `} ` ` ` `else` `{ ` ` ` `if` `(pq2.size() == 0) { ` ` ` `break` `; ` ` ` `} ` ` ` `pq2.pop(); ` ` ` `} ` ` ` `} ` ` ` `return` `(c); ` `} ` ` ` `// Driver code ` `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)); ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the maximum count of ` `// values that follow the given condition ` ` ` `import` `java.util.*; ` `public` `class` `GFG { ` ` ` ` ` `// Function to find the maximum count of ` ` ` `// values that follow the given condition ` ` ` `static` `int` `check(` `int` `A[], ` `int` `B[], ` `int` `N) ` ` ` `{ ` ` ` ` ` `// Initializing the max-heap for the array A[] ` ` ` `PriorityQueue<Integer> pq1 ` ` ` `= ` `new` `PriorityQueue<Integer>( ` `Collections.reverseOrder()); ` ` ` ` ` `// Initializing the max-heap for the array B[] ` ` ` `PriorityQueue<Integer> pq2 ` ` ` `= ` `new` `PriorityQueue<Integer>( ` `Collections.reverseOrder()); ` ` ` ` ` `// Adding the values of A[] into max heap ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `pq1.add(A[i]); ` ` ` `} ` ` ` ` ` `// Adding the values of B[] into max heap ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `pq2.add(B[i]); ` ` ` `} ` ` ` ` ` `// Counter variable ` ` ` `int` `c = ` `0` `; ` ` ` ` ` `// Loop to iterate through the heap ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` ` ` `// Comparing the values at the top. ` ` ` `// If the value of heap A[] is greater, ` ` ` `// then counter is incremented ` ` ` `if` `(pq1.peek().compareTo(pq2.peek()) == ` `1` `) { ` ` ` `c++; ` ` ` `pq1.poll(); ` ` ` `pq2.poll(); ` ` ` `} ` ` ` `else` `{ ` ` ` `if` `(pq2.size() == ` `0` `) { ` ` ` `break` `; ` ` ` `} ` ` ` `pq2.poll(); ` ` ` `} ` ` ` `} ` ` ` `return` `(c); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find the maximum count of ` `# values that follow the given condition ` `import` `heapq ` ` ` `# Function to find the maximum count of ` `# values that follow the given condition ` `def` `check(A, B,N): ` ` ` ` ` `# Initializing the max-heap for the array A[] ` ` ` `pq1 ` `=` `[] ` ` ` `pq2 ` `=` `[] ` ` ` ` ` `# Adding the values of A[] into max heap ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `heapq.heappush(pq1,` `-` `A[i]) ` ` ` ` ` `# Adding the values of B[] into max heap ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `heapq.heappush(pq2,` `-` `B[i]) ` ` ` ` ` `# Counter variable ` ` ` `c ` `=` `0` ` ` ` ` `# Loop to iterate through the heap ` ` ` `for` `i ` `in` `range` `(N): ` ` ` ` ` `# Comparing the values at the top. ` ` ` `# If the value of heap A[] is greater, ` ` ` `# then counter is incremented ` ` ` `if` `-` `pq1[` `0` `] > ` `-` `pq2[` `0` `]: ` ` ` `c ` `+` `=` `1` ` ` `heapq.heappop(pq1) ` ` ` `heapq.heappop(pq2) ` ` ` ` ` `else` `: ` ` ` `if` `len` `(pq2) ` `=` `=` `0` `: ` ` ` `break` ` ` `heapq.heappop(pq2) ` ` ` `return` `(c) ` ` ` `# Driver code ` `A ` `=` `[ ` `10` `, ` `3` `, ` `7` `, ` `5` `, ` `8` `] ` `B ` `=` `[ ` `8` `, ` `6` `, ` `2` `, ` `5` `, ` `9` `] ` `N ` `=` `len` `(A) ` ` ` `print` `(check(A, B, N)) ` ` ` `# This code is contributed by apurva raj ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N * log(N))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of distinct possible pairs such that the element from A is greater than the element from B
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Number of pairs in an array such that product is greater than sum
- Maximize the number of indices such that element is greater than element to its left
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Rearrange an array such that every odd indexed element is greater than it previous
- Number of positions such that adding K to the element is greater than sum of all other elements
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Sum of all array elements less than X and greater than Y for Q queries
- Queries for elements greater than K in the given index range using Segment Tree
- Number of permutations such that sum of elements at odd index and even index are equal
- Count of contiguous subarrays possible for every index by including the element at that index
- Number of pairs in an array with the sum greater than 0
- Count of subarrays whose maximum element is greater than k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.