Related Articles
Number of pairs in an array such that product is greater than sum
• Difficulty Level : Medium
• Last Updated : 27 Apr, 2018

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
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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]

## C++

 `// Naive C++ program to count number of pairs``// such that their sum is more than product.``#include``using` `namespace` `std;`` ` `// Returns the number of valid pairs``int` `countPairs (``int` `arr[], ``int` `n)``{    ``    ``int` `ans = 0;  ``// initializing answer`` ` `    ``// Traversing the array. For each array``    ``// element, checking its predecessors that``    ``// follow the condition``    ``for` `(``int` `i = 0; i= 0; j--)``            ``if` `(arr[i]*arr[j] > arr[i] + arr[j])``                ``ans++;``    ``return` `ans;``}`` ` `// Driver function``int` `main()``{``    ``int` `arr[] = {3, 4, 5};``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr);``    ``cout << countPairs(arr, n);``    ``return` `0;``}`

## Java

 `// Naive java program to count number of pairs``// such that their sum is more than product.``import` `java.*;`` ` `public` `class` `GFG ``{``     ` `    ``// Returns the number of valid pairs``    ``static` `int` `countPairs (``int` `arr[], ``int` `n)``    ``{ ``        ``int` `ans = ``0``; ``// initializing answer``     ` `        ``// Traversing the array. For each array``        ``// element, checking its predecessors that``        ``// follow the condition``        ``for` `(``int` `i = ``0``; i= ``0``; j--)``                ``if` `(arr[i]*arr[j] > arr[i] + arr[j])``                    ``ans++;``        ``return` `ans;``    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = {``3``, ``4``, ``5``};``        ``int` `n = arr.length;``        ``System.out.println(countPairs(arr, n));``    ``}``}`` ` `// This code is contributed by Sam007`

## Python3

 `# Naive Python program to count number``# of pairs such that their sum is more``# than product.`` ` `# Returns the number of valid pairs``def` `countPairs(arr, n):``     ` `    ``# initializing answer``    ``ans ``=` `0``     ` `    ``# Traversing the array. For each``    ``# array element, checking its ``    ``# predecessors that follow the``    ``# condition``    ``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``     ` `# Driver program to test above function.``arr ``=` `[``3``, ``4``, ``5``]``n ``=` `len``(arr) ``k ``=` `countPairs(arr, n)``print``(k)``     ` `# This code is contributed by Sam007.`

## C#

 `// Naive C# program to count number of pairs``// such that their sum is more than product.``using` `System;``         ` `public` `class` `GFG ``{``    ``// Returns the number of valid pairs``    ``static` `int` `countPairs (``int` `[]arr, ``int` `n)``    ``{ ``        ``int` `ans = 0; ``// initializing answer``     ` `        ``// Traversing the array. For each array``        ``// element, checking its predecessors that``        ``// follow the condition``        ``for` `(``int` `i = 0; i= 0; j--)``                ``if` `(arr[i]*arr[j] > arr[i] + arr[j])``                    ``ans++;``        ``return` `ans;``    ``}``     ` `    ``// driver program``    ``public` `static` `void` `Main() ``    ``{``        ``int` `[]arr = {3, 4, 5};``        ``int` `n = arr.Length;``        ``Console.Write( countPairs(arr, n));``    ``}``}`` ` `// This code is contributed by Sam007`

## PHP

 `= 0; ``\$j``--)``            ``if` `(``\$arr``[``\$i``] * ``\$arr``[``\$j``] > ``                ``\$arr``[``\$i``] + ``\$arr``[``\$j``])``                ``\$ans``++;``    ``return` `\$ans``;``}`` ` `// Driver Code``\$arr` `= ``array``(3, 4, 5);``\$n` `= sizeof(``\$arr``);``echo``(countPairs(``\$arr``, ``\$n``));`` ` `// This code is contributed by Ajit.``?>`

Output:

```3
```

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

## C++

 `// C++ implementation of efficient approach``// to count valid pairs.``#include``using` `namespace` `std;`` ` `// returns the number of valid pairs``int` `CountPairs (``int` `arr[], ``int` `n)``{``    ``// traversing the array, counting the``    ``// number of 2s and greater than 2``    ``// in array``    ``int` `twoCount = 0, twoGrCount = 0;``    ``for` `(``int` `i = 0; i 2)``            ``twoGrCount++;``    ``}``    ``return` `twoCount*twoGrCount +``          ``(twoGrCount*(twoGrCount-1))/2;``}`` ` `// Driver function``int` `main()``{``    ``int` `arr[] = {3, 4, 5};``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr);``    ``cout << CountPairs(arr, n);``    ``return` `0;``}`

## Java

 `// Java implementation of efficient approach``// to count valid pairs.``import` `java.*;`` ` `public` `class` `GFG ``{``    ``// Returns the number of valid pairs``    ``static` `int` `countPairs (``int` `arr[], ``int` `n)``    ``{ ``        ``// traversing the array, counting the``        ``// number of 2s and greater than 2``        ``// in array``        ``int` `twoCount = ``0``, twoGrCount = ``0``;``        ``for` `(``int` `i = ``0``; i ``2``)``            ``twoGrCount++;``        ``}``        ``return` `twoCount*twoGrCount +``        ``(twoGrCount*(twoGrCount-``1``))/``2``;``    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = {``3``, ``4``, ``5``};``        ``int` `n = arr.length;``        ``System.out.println(countPairs(arr, n));``    ``}``}`` ` `// This code is contributed by Sam007`

## Python3

 `# python implementation of efficient approach``# to count valid pairs.`` ` `# returns the number of valid pairs``def` `CountPairs (arr,n):``     ` `    ``# traversing the array, counting the``    ``# number of 2s and greater than 2``    ``# in array``    ``twoCount ``=` `0``    ``twoGrCount ``=` `0``    ``for` `i ``in` `range``(``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.`

## C#

 `// C# implementation of efficient approach``// to count valid pairs.``using` `System;``         ` `public` `class` `GFG ``{``    ``// Returns the number of valid pairs``    ``static` `int` `countPairs (``int` `[]arr, ``int` `n) {``         ` `    ``// traversing the array, counting the``    ``// number of 2s and greater than 2``    ``// in array``    ``int` `twoCount = 0, twoGrCount = 0;``    ``for` `(``int` `i = 0; i 2)``            ``twoGrCount++;``    ``}``    ``return` `twoCount*twoGrCount +``           ``(twoGrCount*(twoGrCount-1))/2;``    ``}``     ` `    ``// driver program``    ``public` `static` `void` `Main() ``    ``{``        ``int` `[]arr = {3, 4, 5};``        ``int` `n = arr.Length;``        ``Console.Write( countPairs(arr, n));``    ``}``}`` ` `// This code is contributed by Sam007`

## PHP

 ` 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.``?>`

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up