Number of pairs in an array such that product is greater than sum
Last Updated :
13 Jul, 2022
Given a array a[] of non-negative integers. Count the number of pairs (i, j) in the array such that a[i] + a[j] < a[i]*a[j]. (the pair (i, j) and (j, i) are considered same and i should not be equal to j)
Examples:
Input : a[] = {3, 4, 5}
Output : 3
Pairs are (3, 4) , (4, 5) and (3,5)
Input : a[] = {1, 1, 1}
Output : 0
Naive approach: For each value a[i] count the number of a[j] (i > j) such that a[i]*a[j] > a[i] + a[j]
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countPairs ( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i<n; i++)
for ( int j = i-1; j>= 0; j--)
if (arr[i]*arr[j] > arr[i] + arr[j])
ans++;
return ans;
}
int main()
{
int arr[] = {3, 4, 5};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
import java.*;
public class GFG
{
static int countPairs ( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i<n; i++)
for ( int j = i- 1 ; j>= 0 ; j--)
if (arr[i]*arr[j] > arr[i] + arr[j])
ans++;
return ans;
}
public static void main(String args[])
{
int arr[] = { 3 , 4 , 5 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
ans = 0
for i in range ( 0 , n):
j = i - 1
while (j > = 0 ):
if (arr[i] * arr[j] >
arr[i] + arr[j]):
ans = ans + 1
j = j - 1
return ans
arr = [ 3 , 4 , 5 ]
n = len (arr)
k = countPairs(arr, n)
print (k)
|
C#
using System;
public class GFG
{
static int countPairs ( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i<n; i++)
for ( int j = i-1; j>= 0; j--)
if (arr[i]*arr[j] > arr[i] + arr[j])
ans++;
return ans;
}
public static void Main()
{
int []arr = {3, 4, 5};
int n = arr.Length;
Console.Write( countPairs(arr, n));
}
}
|
PHP
<?php
function countPairs ( $arr , $n )
{
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i - 1; $j >= 0; $j --)
if ( $arr [ $i ] * $arr [ $j ] >
$arr [ $i ] + $arr [ $j ])
$ans ++;
return $ans ;
}
$arr = array (3, 4, 5);
$n = sizeof( $arr );
echo (countPairs( $arr , $n ));
?>
|
Javascript
<script>
function countPairs(arr, n)
{
let ans = 0;
for (let i = 0; i<n; i++)
for (let j = i-1; j>= 0; j--)
if (arr[i]*arr[j] > (arr[i] + arr[j]))
ans++;
return ans;
}
let arr = [3, 4, 5];
let n = arr.length;
document.write( countPairs(arr, n));
</script>
|
Efficient approach:
When a[i] = 0 : a[i]*a[j] = 0 and a[i] + a[j] >= 0 so if a[i] = 0 no pairs can be found.
When a[i] = 1 : a[i]*a[j] = a[j] and a[i] + a[j] = 1 + a[j], so no pairs can be found when a[i] = 1
When a[i] = 2 and a[j] = 2 : a[i]*a[j] = a[i] + a[j] = 4
When a[i] = 2 and a[j] > 2 or a[i] > 2 and a[j] >= 2 : All such pairs are valid.
To solve this problem, count the number of 2s in the array say twoCount. Count the numbers greater than 2 in the array say twoGreaterCount. Answer will be twoCount * twoGreaterCount + twoGreaterCount * (twoGreaterCount-1)/2
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int CountPairs ( int arr[], int n)
{
int twoCount = 0, twoGrCount = 0;
for ( int i = 0; i<n; i++)
{
if (arr[i] == 2)
twoCount++;
else if (arr[i] > 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
int main()
{
int arr[] = {3, 4, 5};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << CountPairs(arr, n);
return 0;
}
|
Java
import java.*;
public class GFG
{
static int countPairs ( int arr[], int n)
{
int twoCount = 0 , twoGrCount = 0 ;
for ( int i = 0 ; i<n; i++)
{
if (arr[i] == 2 )
twoCount++;
else if (arr[i] > 2 )
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount- 1 ))/ 2 ;
}
public static void main(String args[])
{
int arr[] = { 3 , 4 , 5 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def CountPairs (arr,n):
twoCount = 0
twoGrCount = 0
for i in range ( 0 , n):
if (arr[i] = = 2 ):
twoCount + = 1
else if (arr[i] > 2 ):
twoGrCount + = 1
return ((twoCount * twoGrCount)
+ (twoGrCount * (twoGrCount - 1 )) / 2 )
arr = [ 3 , 4 , 5 ]
n = len (arr)
print ( CountPairs(arr, n))
|
C#
using System;
public class GFG
{
static int countPairs ( int []arr, int n) {
int twoCount = 0, twoGrCount = 0;
for ( int i = 0; i<n; i++)
{
if (arr[i] == 2)
twoCount++;
else if (arr[i] > 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
public static void Main()
{
int []arr = {3, 4, 5};
int n = arr.Length;
Console.Write( countPairs(arr, n));
}
}
|
PHP
<?php
function CountPairs ( $arr , $n )
{
$twoCount = 0; $twoGrCount = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 2)
$twoCount ++;
else if ( $arr [ $i ] > 2)
$twoGrCount ++;
}
return $twoCount * $twoGrCount +
( $twoGrCount * ( $twoGrCount -
1)) / 2;
}
$arr = array (3, 4, 5);
$n = sizeof( $arr );
echo (CountPairs( $arr , $n ));
?>
|
Javascript
<script>
function CountPairs(arr, n)
{
let twoCount = 0, twoGrCount = 0;
for (let i = 0; i<n; i++)
{
if (arr[i] == 2)
twoCount++;
else if (arr[i] > 2)
twoGrCount++;
}
return twoCount*twoGrCount + parseInt((twoGrCount*(twoGrCount-1))/2, 10);
}
let arr = [3, 4, 5];
let n = arr.length;
document.write(CountPairs(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...