Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
Last Updated :
30 Mar, 2022
Given an array arr[] of length N, count the number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j] and 0 <= i < j <= N. It is also given that elements of the array can be any positive integers including zero.
Examples:
Input : arr[] = {2, 0, 3, 2, 0}
Output : 2
Input : arr[] = {1, 2, 3, 4}
Output : 0
Simple solution:
We can generate all possible pairs of the array and count those pairs which satisfy the given condition.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
long countPairs( int arr[], int n)
{
long count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
return count;
}
int main()
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
class GFG {
static long countPairs( int arr[], int n)
{
long count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 2 , 0 , 3 , 2 , 0 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n) :
count = 0 ;
for i in range (n - 1 ) :
for j in range (i + 1 , n) :
if (arr[i] * arr[j] = = arr[i] + arr[j]) :
count + = 1 ;
return count;
if __name__ = = "__main__" :
arr = [ 2 , 0 , 3 , 2 , 0 ];
n = len (arr);
print (countPairs(arr, n));
|
C#
using System;
class GFG {
static long countPairs( int [] arr, int n)
{
long count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 2, 0, 3, 2, 0 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let count = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
return count;
}
let arr = [ 2, 0, 3, 2, 0 ];
let n = arr.length;
document.write(countPairs(arr, n));
|
Output:
2
Time Complexity: O(n2)
Auxiliary Space: O(12)
Efficient Solution:
Taking arr[i] as x and arr[j] as y, we can rewrite the given condition as the following equation.
xy = x + y
xy - x - y = 0
xy - x - y + 1 = 1
x(y - 1) -(y - 1) = 1
(x - 1)(y - 1) = 1
Case 1:
x - 1 = 1 i.e x = 2
y - 1 = 1 i.e y = 2
Case 2:
x - 1 = -1 i.e x = 0
y - 1 = -1 i.e y = 0
So, now we know that the condition arr[i] * arr[j] = arr[i] + arr[j] will satisfy only if either arr[i] = arr[j] = 0 or arr[i] = arr[j] = 2.
All we need to do is to count the occurrence of 2’s and 0’s. We can then get the number of pairs using formula
(count * (count - 1)) / 2
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
long countPairs( int arr[], int n)
{
int countZero = 0;
int countTwo = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
long pair0 = (countZero * (countZero - 1)) / 2;
long pair2 = (countTwo * (countTwo - 1)) / 2;
return pair0 + pair2;
}
int main()
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
class GFG {
static long countPairs( int arr[], int n)
{
int countZero = 0 ;
int countTwo = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 0 )
countZero++;
else if (arr[i] == 2 )
countTwo++;
}
long pair0 = (countZero * (countZero - 1 )) / 2 ;
long pair2 = (countTwo * (countTwo - 1 )) / 2 ;
return pair0 + pair2;
}
public static void main(String[] args)
{
int arr[] = { 2 , 0 , 3 , 2 , 0 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
countZero = 0 ;
countTwo = 0 ;
for i in range (n) :
if (arr[i] = = 0 ) :
countZero + = 1 ;
elif (arr[i] = = 2 ) :
countTwo + = 1 ;
pair0 = (countZero * (countZero - 1 )) / / 2 ;
pair2 = (countTwo * (countTwo - 1 )) / / 2 ;
return pair0 + pair2;
if __name__ = = "__main__" :
arr = [ 2 , 0 , 3 , 2 , 0 ];
n = len (arr);
print (countPairs(arr, n));
|
C#
using System;
class GFG {
static long countPairs( int [] arr, int n)
{
int countZero = 0;
int countTwo = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
long pair0 = (countZero * (countZero - 1)) / 2;
long pair2 = (countTwo * (countTwo - 1)) / 2;
return pair0 + pair2;
}
public static void Main( string [] args)
{
int [] arr = { 2, 0, 3, 2, 0 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Output:
2
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...