Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j]
Given an array arr[] of non-negative integers, the task is to count pairs of indices (i, j such that arr[i] * arr[j] > arr[i] + arr[j] where i < j.
Examples:
Input: arr[] = { 5, 0, 3, 1, 2 }
Output: 3
Input: arr[] = { 1, 1, 1 }
Output: 0
Naive Approach: Run two nested loops and check for every pair whether the condition is satisfied. If the condition is satisfied for any pair then update count = count + 1 and print the count in the end.
Below is the implementation of the above approach:
C++
#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[] = { 5, 0, 3, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class solution
{
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[] = { 5 , 0 , 3 , 1 , 2 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
import math as mt
def countPairs(arr, n):
count = 0
for i in range (n):
for j in range (i + 1 , n):
if arr[i] * arr[j] > arr[i] + arr[j]:
count + = 1
return count
arr = [ 5 , 0 , 3 , 1 , 2 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
public 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;
}
static public void Main (){
int []arr = { 5, 0, 3, 1, 2 };
int n = arr.Length;
Console.WriteLine (countPairs(arr, n));
}
}
|
PHP
<?php
function countPairs( $arr , $n )
{
$count = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
for ( $j = $i + 1; $j < $n ; $j ++)
{
if ( $arr [ $i ] *
$arr [ $j ] > $arr [ $i ] +
$arr [ $j ])
$count ++;
}
}
return $count ;
}
$arr = array ( 5, 0, 3, 1, 2 );
$n = sizeof( $arr ) ;
echo 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 = [ 5, 0, 3, 1, 2 ];
let n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Efficient Approach: Consider the following cases:
1) arr[i] = 0 or arr[i] = 1 and arr[j] = any element
In this case, arr[j] * arr[i] will always be less than arr[i] + arr[j].
Hence we can discard all pairs which have one element either 0 or 1.
2) arr[i] = 2 and arr[j] <= 2
In this case, arr[j] * arr[i] will always be less than or equal to arr[i] + arr[j].
Hence again we can discard all such pairs.
3) arr[i] = 2 and arr[j] > 2
This case will produce valid pairs. If count_2 is the count of ‘2’s and count_others
is the count of elements greater than 2,
then number of pairs will be count_2 * count_others.
4) arr[i] > 2 and arr[j] > 2
This case will also produce valid pairs. Let count_others be the number of elements
greater than 2, then every two elements among these count_others elements
will form a valid pair. Hence the number of pairs will be
Therefore, total count = (count_2 * count_others) + (count_others * (count_others – 1)) / 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long countPairs( const int * arr, int n)
{
int count_2 = 0, count_others = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 2)
count_2++;
else if (arr[i] > 2)
count_others++;
}
long ans
= 1L * count_2 * count_others
+ (1L * count_others * (count_others - 1)) / 2;
return ans;
}
int main()
{
int arr[] = { 5, 0, 3, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
class GFG
{
static long countPairs( int [] arr, int n)
{
int count_2 = 0 , count_others = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == 2 )
{
count_2++;
}
else if (arr[i] > 2 )
{
count_others++;
}
}
long ans = 1L * count_2 * count_others +
(1L * count_others * (count_others - 1 )) / 2 ;
return ans;
}
public static void main(String[] args)
{
int arr[] = { 5 , 0 , 3 , 1 , 2 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
import math as mt
def countPairs(arr, n):
count_2, count_others = 0 , 0
for i in range (n):
if arr[i] = = 2 :
count_2 + = 1
elif arr[i] > 2 :
count_others + = 1
ans = (count_2 * count_others +
(count_others *
(count_others - 1 )) / / 2 )
return ans
arr = [ 5 , 0 , 3 , 1 , 2 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
class GFG
{
static long countPairs( int [] arr, int n)
{
int count_2 = 0, count_others = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
count_2++;
}
else if (arr[i] > 2)
{
count_others++;
}
}
long ans = 1L * count_2 * count_others +
(1L * count_others *
(count_others - 1)) / 2;
return ans;
}
public static void Main()
{
int [] arr = {5, 0, 3, 1, 2};
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
PHP
<?php
function countPairs( $arr , $n )
{
$count_2 = 0; $count_others = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 2)
$count_2 ++;
else if ( $arr [ $i ] > 2)
$count_others ++;
}
$ans = $count_2 * $count_others +
( $count_others *
( $count_others - 1)) / 2;
return $ans ;
}
$arr = array ( 5, 0, 3, 1, 2 );
$n = sizeof( $arr );
echo countPairs( $arr , $n );
?>
|
Javascript
<script>
function countPairs(arr, n)
{
let count_2 = 0, count_others = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] == 2)
{
count_2++;
}
else if (arr[i] > 2)
{
count_others++;
}
}
let ans = count_2 * count_others +
(count_others * (count_others - 1)) / 2;
return ans;
}
let arr = [5, 0, 3, 1, 2];
let n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
13 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...