Given two arrays a[] and b[], both of size N. The task is to count the number of distinct pairs such that (a[i] + a[j] ) > ( b[i] + b[j] ) subject to condition that (j > i).
Examples:
Input: N = 5, a[] = {1, 2, 3, 4, 5}, b[] = {2, 5, 6, 1, 9}
Output: 1
Explanation:
Only one such pair exists and that is (0, 3).
a[0] = 1, a[3] = 4 and a[0] + a[3] = 1 + 4 = 5.
b[0] = 2, b[3] = 1 and b[0] + b[3] = 2 + 1 = 3.
Clearly, 5 > 3 and j > i. Thus (0, 3) is a possible pair.
Input: N = 5, a[] = {2, 4, 2, 7, 8}, b[] = {1, 3, 6, 4, 5}
Output: 6
Naive Approach:
The simplest way is to iterate through every possible pair and if it satisfies the condition, then increment the count. Then, return the count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_pairs( int a[],
int b[], int N)
{
int i, j;
int count = 0;
for (i = 0; i < (N - 1); i++) {
for (j = (i + 1); j < N; j++) {
if ((a[i] + a[j])
> (b[i] + b[j])) {
count++;
}
}
}
return count;
}
int main()
{
int N = 5;
int a[N] = { 1, 2, 3, 4, 5 };
int b[N] = { 2, 5, 6, 1, 9 };
cout << count_pairs(a, b, N)
<< endl;
return 0;
}
|
Java
class GFG{
static int count_pairs( int []a,
int b[], int N)
{
int i, j;
int count = 0 ;
for (i = 0 ; i < (N - 1 ); i++)
{
for (j = (i + 1 ); j < N; j++)
{
if ((a[i] + a[j]) > (b[i] + b[j]))
{
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 5 ;
int a[] = new int []{ 1 , 2 , 3 , 4 , 5 };
int b[] = new int []{ 2 , 5 , 6 , 1 , 9 };
System.out.println(count_pairs(a, b, N));
}
}
|
Python3
def count_pairs(a, b, N):
count = 0 ;
for i in range ( 0 , N - 1 ):
for j in range (i + 1 , N):
if ((a[i] + a[j]) > (b[i] + b[j])):
count + = 1 ;
return count;
N = 5 ;
a = [ 1 , 2 , 3 , 4 , 5 ];
b = [ 2 , 5 , 6 , 1 , 9 ];
print (count_pairs(a, b, N)
|
C#
using System;
class GFG{
static int count_pairs( int []a,
int []b, int N)
{
int i, j;
int count = 0;
for (i = 0; i < (N - 1); i++)
{
for (j = (i + 1); j < N; j++)
{
if ((a[i] + a[j]) > (b[i] + b[j]))
{
count++;
}
}
}
return count;
}
public static void Main()
{
int N = 5;
int []a = new int []{ 1, 2, 3, 4, 5 };
int []b = new int []{ 2, 5, 6, 1, 9 };
Console.Write(count_pairs(a, b, N));
}
}
|
Javascript
<script>
function count_pairs(a, b, N)
{
let i, j;
let count = 0;
for (i = 0; i < (N - 1); i++)
{
for (j = (i + 1); j < N; j++)
{
if ((a[i] + a[j]) >
(b[i] + b[j]))
{
count++;
}
}
}
return count;
}
let N = 5;
let a = [ 1, 2, 3, 4, 5 ];
let b = [ 2, 5, 6, 1, 9 ];
document.write(count_pairs(a, b, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
Follow the steps below to solve the problem:
- Re-arrange the given inequality as:
=> a[i] + a[j] > b[i] + b[j]
=> a[i] – b[i] + a[j] – b[j] > 0
- Initialize another array c[ ] of size N that stores the values of a[i] – b[i].
- Sort the array c[ ].
- Initialise an answer variable to 0. Iterate over the array c[ ].
- For every index i in the array c[ ] do the following operations:
- If c[i] <= 0, simply continue.
- If c[i] > 0, calculate the minimum index position and store the value in a variable pos such that c[pos] + c[i] > 0. The value of pos can be easily found using lower_bound function in C++ STL.
- Add (i – pos) to the answer.
Illustration:
- N = 5, a[] = {1, 2, 3, 4, 5}, b[] = {2, 5, 6, 1, 9}
- The array c[] for this example will be c[] = {-1, -3, -3, 3, -4}
- After sorting, array c[] = {-4, -3, -3, -1, 3}
- The first and only positive value of array c[] is found at index 4.
- pos = lower_bound(-c[4] + 1) = lower_bound(-2) = 3.
- Count of possible pairs = i – pos = 4 – 3 = 1.
Note: Had there been more than one positive number found in the array c[], then find out the pos for each and every positive value in c[] and add the value of i – pos to the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfPairs( int * a,
int * b, int n)
{
int c[n];
for ( int i = 0; i < n; i++) {
c[i] = a[i] - b[i];
}
sort(c, c + n);
int answer = 0;
for ( int i = 1; i < n; i++) {
if (c[i] <= 0)
continue ;
int pos = lower_bound(c, c + n,
-c[i] + 1)
- c;
answer += (i - pos);
}
return answer;
}
int32_t main()
{
int n = 5;
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 2, 5, 6, 1, 9 };
cout << numberOfPairs(a, b, n)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int numberOfPairs( int [] a,
int [] b, int n)
{
int c[] = new int [n];
for ( int i = 0 ; i < n; i++)
{
c[i] = a[i] - b[i];
}
Arrays.sort(c);
int answer = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (c[i] <= 0 )
continue ;
int pos = - 1 ;
for ( int j = 0 ; j < n; j++)
{
if (c[i] + c[j] > 0 )
{
pos = j;
break ;
}
}
answer += (i - pos);
}
return answer;
}
public static void main (String[] args)
{
int n = 5 ;
int a[] = { 1 , 2 , 3 , 4 , 5 };
int b[] = { 2 , 5 , 6 , 1 , 9 };
System.out.println(numberOfPairs(a, b, n));
}
}
|
Python3
from bisect import bisect_left
def numberOfPairs(a, b, n):
c = [ 0 for i in range (n)]
for i in range (n):
c[i] = a[i] - b[i]
c = sorted (c)
answer = 0
for i in range ( 1 , n):
if (c[i] < = 0 ):
continue
pos = bisect_left(c, - c[i] + 1 )
answer + = (i - pos)
return answer
if __name__ = = '__main__' :
n = 5
a = [ 1 , 2 , 3 , 4 , 5 ]
b = [ 2 , 5 , 6 , 1 , 9 ]
print (numberOfPairs(a, b, n))
|
C#
using System;
class GFG{
static int numberOfPairs( int [] a,
int [] b, int n)
{
int [] c = new int [n];
for ( int i = 0; i < n; i++)
{
c[i] = a[i] - b[i];
}
Array.Sort(c);
int answer = 0;
for ( int i = 1; i < n; i++)
{
if (c[i] <= 0)
continue ;
int pos = -1;
for ( int j = 0; j < n; j++)
{
if (c[i] + c[j] > 0)
{
pos = j;
break ;
}
}
answer += (i - pos);
}
return answer;
}
static void Main()
{
int n = 5;
int [] a = { 1, 2, 3, 4, 5 };
int [] b = { 2, 5, 6, 1, 9 };
Console.WriteLine(numberOfPairs(a, b, n));
}
}
|
Javascript
<script>
function numberOfPairs(a, b, n)
{
let c = new Array(n);
for (let i = 0; i < n; i++)
{
c[i] = a[i] - b[i];
}
c.sort( function (a, b){ return a - b});
let answer = 0;
for (let i = 1; i < n; i++)
{
if (c[i] <= 0)
continue ;
let pos = -1;
for (let j = 0; j < n; j++)
{
if (c[i] + c[j] > 0)
{
pos = j;
break ;
}
}
answer += (i - pos);
}
return answer;
}
let n = 5;
let a = [ 1, 2, 3, 4, 5 ];
let b = [ 2, 5, 6, 1, 9 ];
document.write(numberOfPairs(a, b, n));
</script>
|
Time Complexity: O( N * log(N) ), where N is the number of elements in array.
Auxiliary Space: O(N)