Given two given arrays **A** and **B** of equal length, the task is to find the maximum number of distinct pairs of elements that can be chosen such that the element from A is strictly greater than the element from B.

**Examples:**

Input:

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

Output:2

Explanation:

(30, 25) and (50, 40) are the two possible pairs.

Input:

A[]={20, 25, 60} , B[]={25, 60, 40}

Output:1

Explanation:

(60, 25) or (60, 40) can be the required pair.

**Approach:** In order to solve this problem we need to adopt the following approach:

- Sort both the arrays.
- Traverse the entire length of array A and check if the current element in A is greater than the element in B currently pointed at B.
- If so, point to the next element in both the arrays. Otherwise, move to the next element in A and check if it is greater than the element currently pointed at B.
- The number of elements traversed in array B after the entire traversal of array A gives the required answer.

Illustration:

Let us consider the two following arrays:

A[] = { 30, 28, 45, 22 } , B[] = { 35, 25, 22, 48 }

After sorting, the arrays appear to be

A[] = { 22, 28, 30, 45 } , B[] = { 22, 25, 35, 48}After the first iteration, since A[0] is not greater than B[0], we move to A[1].

After the second iteration, we move to B[1] as A[1] is greater than B[0].

After the third iteration, we move to B[2] as A[2] is greater than B[1].

Similarly, A[3] is greater than B[2] and we move to B[3].

Hence the number of elements traversed in B,i.e. 3 is the final answer.

The possible pairs are (28,22), (30,25) are (45, 35).

Below is the implementation of the above approach:

## C++

`// C++ Program to count number of distinct ` `// pairs possible from the two arrays ` `// such that element selected from one array is ` `// always greater than the one selected from ` `// the other array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of pairs ` `int` `countPairs(vector<` `int` `> A, ` ` ` `vector<` `int` `> B) ` `{ ` ` ` `int` `n = A.size(); ` ` ` ` ` `sort(A.begin(),A.end()); ` ` ` `sort(B.begin(),B.end()); ` ` ` ` ` `int` `ans = 0, i; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(A[i] > B[ans]) { ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> A = { 30, 28, 45, 22 }; ` ` ` `vector<` `int` `> B = { 35, 25, 22, 48 }; ` ` ` ` ` `cout << countPairs(A,B); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of distinct ` `// pairs possible from the two arrays such ` `// that element selected from one array is ` `// always greater than the one selected from ` `// the other array ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return the count ` `// of pairs ` `static` `int` `countPairs(` `int` `[] A, ` ` ` `int` `[] B) ` `{ ` ` ` `int` `n = A.length; ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `Arrays.sort(A); ` ` ` `Arrays.sort(B); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(A[i] > B[ans]) ` ` ` `{ ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[] A = { ` `30` `, ` `28` `, ` `45` `, ` `22` `}; ` ` ` `int` `[] B = { ` `35` `, ` `25` `, ` `22` `, ` `48` `}; ` ` ` ` ` `System.out.print(countPairs(A, B)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number of distinct ` `# pairs possible from the two arrays ` `# such that element selected from one array is ` `# always greater than the one selected from ` `# the other array ` ` ` `# Function to return the count ` `# of pairs ` `def` `countPairs(A, B): ` ` ` ` ` `n ` `=` `len` `(A) ` ` ` ` ` `A.sort() ` ` ` `B.sort() ` ` ` ` ` `ans ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(A[i] > B[ans]): ` ` ` `ans ` `+` `=` `1` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `A ` `=` `[` `30` `, ` `28` `, ` `45` `, ` `22` `] ` ` ` `B ` `=` `[` `35` `, ` `25` `, ` `22` `, ` `48` `] ` ` ` ` ` `print` `(countPairs(A, B)) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of distinct ` `// pairs possible from the two arrays such ` `// that element selected from one array is ` `// always greater than the one selected from ` `// the other array ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to return the count ` `// of pairs ` `static` `int` `countPairs(` `int` `[] A, ` ` ` `int` `[] B) ` `{ ` ` ` `int` `n = A.Length; ` ` ` `int` `ans = 0; ` ` ` ` ` `Array.Sort(A); ` ` ` `Array.Sort(B); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(A[i] > B[ans]) ` ` ` `{ ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]A = { 30, 28, 45, 22 }; ` ` ` `int` `[]B = { 35, 25, 22, 48 }; ` ` ` ` ` `Console.Write(countPairs(A, B)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

3

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 pairs from arrays A and B such that element in A is greater than element in B at that index
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- 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
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- All possible co-prime distinct element pairs within a range [L, R]
- Count of pairs between two arrays such that the sums are distinct
- Count elements such that there are exactly X elements with values greater than or equal to X
- 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 numbers in given range such that sum of even digits is greater than sum of odd digits
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- 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
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Find unique pairs such that each element is less than or equal to N

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.