Number of pairs in an array such that product is greater than sum

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)

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

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ implementation of efficient approach

// to count valid pairs.

#include<bits/stdc++.h>

usingnamespacestd;

// returns the number of valid pairs

intCountPairs (intarr[], intn)

{

// traversing the array, counting the

// number of 2s and greater than 2

// in array

inttwoCount = 0, twoGrCount = 0;

for(inti = 0; i<n; i++)

{

if(arr[i] == 2)

twoCount++;

elseif(arr[i] > 2)

twoGrCount++;

}

returntwoCount*twoGrCount +

(twoGrCount*(twoGrCount-1))/2;

}

// Driver function

intmain()

{

intarr[] = {3, 4, 5};

intn = sizeof(arr)/sizeof(arr[0]);

cout << CountPairs(arr, n);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java implementation of efficient approach

// to count valid pairs.

importjava.*;

publicclassGFG

{

// Returns the number of valid pairs

staticintcountPairs (intarr[], intn)

{

// traversing the array, counting the

// number of 2s and greater than 2

// in array

inttwoCount = 0, twoGrCount = 0;

for(inti = 0; i<n; i++)

{

if(arr[i] == 2)

twoCount++;

elseif(arr[i] > 2)

twoGrCount++;

}

returntwoCount*twoGrCount +

(twoGrCount*(twoGrCount-1))/2;

}

// Driver code

publicstaticvoidmain(String args[])

{

intarr[] = {3, 4, 5};

intn = arr.length;

System.out.println(countPairs(arr, n));

}

}

// This code is contributed by Sam007

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# python implementation of efficient approach

# to count valid pairs.

# returns the number of valid pairs

defCountPairs (arr,n):

# traversing the array, counting the

# number of 2s and greater than 2

# in array

twoCount =0

twoGrCount =0

fori inrange(0, n):

if(arr[i] ==2):

twoCount +=1

elif(arr[i] > 2):

twoGrCount +=1

return((twoCount *twoGrCount)

+(twoGrCount *(twoGrCount -1)) /2)

# Driver function

arr =[3, 4, 5]

n =len(arr)

print( CountPairs(arr, n))

# This code is contributed by Sam007.

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# implementation of efficient approach

// to count valid pairs.

usingSystem;

publicclassGFG

{

// Returns the number of valid pairs

staticintcountPairs (int[]arr, intn) {

// traversing the array, counting the

// number of 2s and greater than 2

// in array

inttwoCount = 0, twoGrCount = 0;

for(inti = 0; i<n; i++)

{

if(arr[i] == 2)

twoCount++;

elseif(arr[i] > 2)

twoGrCount++;

}

returntwoCount*twoGrCount +

(twoGrCount*(twoGrCount-1))/2;

}

// driver program

publicstaticvoidMain()

{

int[]arr = {3, 4, 5};

intn = arr.Length;

Console.Write( countPairs(arr, n));

}

}

// This code is contributed by Sam007

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP implementation of

// efficient approach

// to count valid pairs.

// returns the number

// of valid pairs

functionCountPairs ($arr, $n)

{

// traversing the array, counting

// the number of 2s and greater

// than 2 in array

$twoCount= 0; $twoGrCount= 0;

for($i= 0; $i< $n; $i++)

{

if($arr[$i] == 2)

$twoCount++;

elseif($arr[$i] > 2)

$twoGrCount++;

}

return$twoCount* $twoGrCount+

($twoGrCount* ($twoGrCount-

1)) / 2;

}

// Driver Code

$arr= array(3, 4, 5);

$n= sizeof($arr);

echo(CountPairs($arr, $n));

// This code is contributed by Ajit.

?>

chevron_right

filter_none

Output:

3

This article is contributed by Ayush Jha. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.