Given two integer arrays **A[]** and **B[]** of equal sizes, the task is to find the number of pairs of indices {i, j} in the arrays such that **A[i] + A[j] > B[i] + B[j]** and **i < j**.

**Examples:**

Input:A[] = {4, 8, 2, 6, 2}, B[] = {4, 5, 4, 1, 3}

Output:7

Explanation:

There are a total of 7 pairs of indices {i, j} in the array following the condition. They are:

{0, 1}: A[0] + A[1] > B[0] + B[1]

{0, 3}: A[0] + A[3] > B[0] + B[3]

{1, 2}: A[1] + A[2] > B[1] + B[2]

{1, 3}: A[1] + A[3] > B[1] + B[3]

{1, 4}: A[1] + A[4] > B[1] + B[4]

{2, 3}: A[2] + A[3] > B[2] + B[3]

{3, 4}: A[3] + A[4] > B[3] + B[4]

Input:A[] = {1, 3, 2, 4}, B[] = {1, 3, 2, 4}

Output:0

Explanation:

No such possible pairs of {i, j} can be found that satisfies the given condition

**Naive Approach:** The naive approach is to consider all the possible pairs of {i, j} in the given arrays and check if **A[i] + A[j] > B[i] + B[j]**. This can be done by using the concept of nested loops.

**Time Complexity:** O(N^{2})

**Efficient Approach:** The key observation from the problem is that the given condition can also be visualised as (ai-bi) + (aj-bj)> 0 so we can make another array to store the difference of both arrays. let this array be D . Therefore, the problem reduces to finding pairs with Di+Dj>0. Now we can sort D array and for each corresponding element Di we will find the no of good pairs that Di can make and add this no of pairs to a count variable.For each element Di to find the no of good pairs it can make we can use the upper_bound function of the standard template library to find the upper bound of -Di. since the array is sorted so all elements present after -Di will also make good pair with Di .thus,if upper bound of -Di is x and n be the total size of array then total pairs corresponding to Di will be n-x. This approach takes O(NlogN) time.

- The given condition in the question can be rewritten as:
A[i] + A[j] > B[i] + B[j] A[i] + A[j] - B[i] - B[j] > 0 (A[i] - B[i]) + (A[j] - B[j]) > 0

- Create another array, say D, to store the difference between elements at the corresponding index in both array, i.e.
D[i] = A[i] - B[i]

- Now to make sure that the constraint i < j is satisfied, sort the difference array D, so that each element i is smaller than elements to its right.
- If at some index i, the value in the difference array D is negative, then we only need to find the
**nearest position ‘j’**at which the value is just greater than**-D[i]**, so that on summation the value becomes**> 0**.Inorder to find such index ‘j’, upper_bound() function or Binary Search can be used, since the array is sorted.

Below is the implementation of the above approach:

## C++

`// C++ program to find the number of indices pair ` `// such that pair sum from first Array ` `// is greater than second Array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to get the number of pairs of indices ` `// {i, j} in the given two arrays A and B such that ` `// A[i] + A[j] > B[i] + B[j] ` `int` `getPairs(vector<` `int` `> A, vector<` `int` `> B, ` `int` `n) ` `{ ` ` ` `// Intitializing the difference array D ` ` ` `vector<` `int` `> D(n); ` ` ` ` ` `// Computing the difference between the ` ` ` `// elements at every index and storing ` ` ` `// it in the array D ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `D[i] = A[i] - B[i]; ` ` ` `} ` ` ` ` ` `// Sort the array D ` ` ` `sort(D.begin(), D.end()); ` ` ` ` ` `// Variable to store the total ` ` ` `// number of pairs that satisfy ` ` ` `// the given condition ` ` ` `long` `long` `total = 0; ` ` ` ` ` `// Loop to iterate through the difference ` ` ` `// array D and find the total number ` ` ` `// of pairs of indices that follow the ` ` ` `// given condition ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` ` ` `// If the value at the index i is positive, ` ` ` `// then it remains positive for any pairs ` ` ` `// with j such that j > i. ` ` ` `if` `(D[i] > 0) { ` ` ` `total += n - i - 1; ` ` ` `} ` ` ` ` ` `// If the value at that index is negative ` ` ` `// then we need to find the index of the ` ` ` `// value just greater than -D[i] ` ` ` `else` `{ ` ` ` `int` `k = upper_bound(D.begin(), ` ` ` `D.end(), -D[i]) ` ` ` `- D.begin(); ` ` ` `total += n - k; ` ` ` `} ` ` ` `} ` ` ` `return` `total; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `vector<` `int` `> A; ` ` ` `vector<` `int` `> B; ` ` ` ` ` `A.push_back(4); ` ` ` `A.push_back(8); ` ` ` `A.push_back(2); ` ` ` `A.push_back(6); ` ` ` `A.push_back(2); ` ` ` ` ` `B.push_back(4); ` ` ` `B.push_back(5); ` ` ` `B.push_back(4); ` ` ` `B.push_back(1); ` ` ` `B.push_back(3); ` ` ` ` ` `cout << getPairs(A, B, n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of indices pair ` `// such that pair sum from first Array ` `// is greater than second Array ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to get the number of pairs of indices ` `// {i, j} in the given two arrays A and B such that ` `// A[i] + A[j] > B[i] + B[j] ` `static` `long` `getPairs(Vector<Integer> A, Vector<Integer> B, ` `int` `n) ` `{ ` ` ` `// Intitializing the difference array D ` ` ` `int` `[]D = ` `new` `int` `[n]; ` ` ` ` ` `// Computing the difference between the ` ` ` `// elements at every index and storing ` ` ` `// it in the array D ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `D[i] = A.get(i) - B.get(i); ` ` ` `} ` ` ` ` ` `// Sort the array D ` ` ` `Arrays.sort(D); ` ` ` ` ` `// Variable to store the total ` ` ` `// number of pairs that satisfy ` ` ` `// the given condition ` ` ` `long` `total = ` `0` `; ` ` ` ` ` `// Loop to iterate through the difference ` ` ` `// array D and find the total number ` ` ` `// of pairs of indices that follow the ` ` ` `// given condition ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) { ` ` ` ` ` `// If the value at the index i is positive, ` ` ` `// then it remains positive for any pairs ` ` ` `// with j such that j > i. ` ` ` `if` `(D[i] > ` `0` `) { ` ` ` `total += n - i - ` `1` `; ` ` ` `} ` ` ` ` ` `// If the value at that index is negative ` ` ` `// then we need to find the index of the ` ` ` `// value just greater than -D[i] ` ` ` `else` `{ ` ` ` `int` `k = upper_bound(D,` `0` `, D.length, -D[i]); ` ` ` `total += n - k; ` ` ` `} ` ` ` `} ` ` ` `return` `total; ` `} ` `static` `int` `upper_bound(` `int` `[] a, ` `int` `low, ` ` ` `int` `high, ` `int` `element) ` `{ ` ` ` `while` `(low < high){ ` ` ` `int` `middle = low + (high - low)/` `2` `; ` ` ` `if` `(a[middle] > element) ` ` ` `high = middle; ` ` ` `else` ` ` `low = middle + ` `1` `; ` ` ` `} ` ` ` `return` `low; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `Vector<Integer> A = ` `new` `Vector<Integer>(); ` ` ` `Vector<Integer> B= ` `new` `Vector<Integer>(); ` ` ` ` ` `A.add(` `4` `); ` ` ` `A.add(` `8` `); ` ` ` `A.add(` `2` `); ` ` ` `A.add(` `6` `); ` ` ` `A.add(` `2` `); ` ` ` ` ` `B.add(` `4` `); ` ` ` `B.add(` `5` `); ` ` ` `B.add(` `4` `); ` ` ` `B.add(` `1` `); ` ` ` `B.add(` `3` `); ` ` ` ` ` `System.out.print(getPairs(A, B, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the number of indices pair ` `# such that pair sum from first Array ` `# is greater than second Array ` `import` `bisect ` ` ` `# Function to get the number of pairs of indices ` `# {i, j} in the given two arrays A and B such that ` `# A[i] + A[j] > B[i] + B[j] ` `def` `getPairs(A, B, n): ` ` ` ` ` `# Intitializing the difference array D ` ` ` `D ` `=` `[` `0` `]` `*` `(n) ` ` ` ` ` `# Computing the difference between the ` ` ` `# elements at every index and storing ` ` ` `# it in the array D ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `D[i] ` `=` `A[i] ` `-` `B[i] ` ` ` ` ` `# Sort the array D ` ` ` `D.sort() ` ` ` ` ` `# Variable to store the total ` ` ` `# number of pairs that satisfy ` ` ` `# the given condition ` ` ` `total ` `=` `0` ` ` ` ` `# Loop to iterate through the difference ` ` ` `# array D and find the total number ` ` ` `# of pairs of indices that follow the ` ` ` `# given condition ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` ` ` `# If the value at the index i is positive, ` ` ` `# then it remains positive for any pairs ` ` ` `# with j such that j > i. ` ` ` `if` `(D[i] > ` `0` `): ` ` ` `total ` `+` `=` `n ` `-` `i ` `-` `1` ` ` ` ` `# If the value at that index is negative ` ` ` `# then we need to find the index of the ` ` ` `# value just greater than -D[i] ` ` ` `else` `: ` ` ` `k ` `=` `bisect.bisect_right(D, ` `-` `D[i], ` `0` `, ` `len` `(D)) ` ` ` `total ` `+` `=` `n ` `-` `k ` ` ` `return` `total ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` ` ` `A ` `=` `[] ` ` ` `B ` `=` `[] ` ` ` ` ` `A.append(` `4` `); ` ` ` `A.append(` `8` `); ` ` ` `A.append(` `2` `); ` ` ` `A.append(` `6` `); ` ` ` `A.append(` `2` `); ` ` ` ` ` `B.append(` `4` `); ` ` ` `B.append(` `5` `); ` ` ` `B.append(` `4` `); ` ` ` `B.append(` `1` `); ` ` ` `B.append(` `3` `); ` ` ` ` ` `print` `(getPairs(A, B, n)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of indices pair ` `// such that pair sum from first Array ` `// is greater than second Array ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to get the number of pairs of indices ` `// {i, j} in the given two arrays A and B such that ` `// A[i] + A[j] > B[i] + B[j] ` `static` `long` `getPairs(List<` `int` `> A, List<` `int` `> B, ` `int` `n) ` `{ ` ` ` `// Intitializing the difference array D ` ` ` `int` `[]D = ` `new` `int` `[n]; ` ` ` ` ` `// Computing the difference between the ` ` ` `// elements at every index and storing ` ` ` `// it in the array D ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `D[i] = A[i] - B[i]; ` ` ` `} ` ` ` ` ` `// Sort the array D ` ` ` `Array.Sort(D); ` ` ` ` ` `// Variable to store the total ` ` ` `// number of pairs that satisfy ` ` ` `// the given condition ` ` ` `long` `total = 0; ` ` ` ` ` `// Loop to iterate through the difference ` ` ` `// array D and find the total number ` ` ` `// of pairs of indices that follow the ` ` ` `// given condition ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` ` ` `// If the value at the index i is positive, ` ` ` `// then it remains positive for any pairs ` ` ` `// with j such that j > i. ` ` ` `if` `(D[i] > 0) { ` ` ` `total += n - i - 1; ` ` ` `} ` ` ` ` ` `// If the value at that index is negative ` ` ` `// then we need to find the index of the ` ` ` `// value just greater than -D[i] ` ` ` `else` `{ ` ` ` `int` `k = upper_bound(D,0, D.Length, -D[i]); ` ` ` `total += n - k; ` ` ` `} ` ` ` `} ` ` ` `return` `total; ` `} ` `static` `int` `upper_bound(` `int` `[] a, ` `int` `low, ` ` ` `int` `high, ` `int` `element) ` `{ ` ` ` `while` `(low < high){ ` ` ` `int` `middle = low + (high - low)/2; ` ` ` `if` `(a[middle] > element) ` ` ` `high = middle; ` ` ` `else` ` ` `low = middle + 1; ` ` ` `} ` ` ` `return` `low; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 5; ` ` ` `List<` `int` `> A = ` `new` `List<` `int` `>(); ` ` ` `List<` `int` `> B= ` `new` `List<` `int` `>(); ` ` ` ` ` `A.Add(4); ` ` ` `A.Add(8); ` ` ` `A.Add(2); ` ` ` `A.Add(6); ` ` ` `A.Add(2); ` ` ` ` ` `B.Add(4); ` ` ` `B.Add(5); ` ` ` `B.Add(4); ` ` ` `B.Add(1); ` ` ` `B.Add(3); ` ` ` ` ` `Console.Write(getPairs(A, B, n)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity Analysis:**

- The sorting of the array takes
**O(N * log(N))**time. - The time taken to find the index which is just greater than a specific value is
**O(Log(N))**. Since in the worst case, this can be executed for**N**elements in the array, the overall time complexity for this is**O(N * log(N))**. - Therefore, the overall time complexity is
**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:

- Maximize the number of indices such that element is greater than element to its left
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Find just strictly greater element from first array for each element in second array
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Smallest subarray of size greater than K with sum greater than a given value
- Longest Subarray with first element greater than or equal to Last element
- Number of positions such that adding K to the element is greater than sum of all other elements
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Check if maximum difference between indices of Non-Zero Elements is greater than X
- Count of strings in the first array which are smaller than every string in the second array
- Number of pairs in an array such that product is greater than sum
- Longest subsequence with first and last element greater than all other elements
- Rearrange an array such that every odd indexed element is greater than it previous
- Sum of all array elements less than X and greater than Y for Q queries
- Largest subset having with sum less than equal to sum of respective indices
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first

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.