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: 3Input: 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++
// C++ program to count pairs (i, j) // such that arr[i] * arr[j] > arr[i] + arr[j] #include <bits/stdc++.h> using namespace std; // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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 condition is satisfied if (arr[i] * arr[j] > arr[i] + arr[j]) count++; } } return count; } // Driver code int main() { int arr[] = { 5, 0, 3, 1, 2 }; int n = sizeof(arr) / sizeof(arr[0]); cout << countPairs(arr, n); return 0; } |
Java
// Java program to count pairs (i, j) // such that arr[i] * arr[j] > arr[i] + arr[j] import java.util.*; class solution { // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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 condition is satisfied if (arr[i] * arr[j] > arr[i] + arr[j]) count++; } } return count; } // Driver code public static void main(String args[]) { int arr[] = { 5, 0, 3, 1, 2 }; int n = arr.length; System.out.println(countPairs(arr, n)); } } // This code is contributed by // Surendra_Gangwar |
Python3
# Python3 program to count pairs(i,j) # such that arr[i]*arr[j]>arr[i]+arr[j] import math as mt # function to return the count of pairs # such that arr[i]*arr[j]>arr[i]+arr[j] def countPairs(arr, n): count = 0 for i in range(n): for j in range(i + 1, n): # if condition is satisified if arr[i] * arr[j] > arr[i] + arr[j]: count += 1 return count # Driver code arr = [5, 0, 3, 1, 2] n = len(arr) print(countPairs(arr, n)) # This code is contributed # by Mohit Kumar 29 |
C#
// C# program to count pairs (i, j) // such that arr[i] * arr[j] > arr[i] + arr[j] using System; public class GFG{ // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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 condition is satisfied if (arr[i] * arr[j] > arr[i] + arr[j]) count++; } } return count; } // Driver code static public void Main (){ int []arr = { 5, 0, 3, 1, 2 }; int n = arr.Length; Console.WriteLine (countPairs(arr, n)); } } |
PHP
<?php // PHP program to count pairs (i, j) // such that arr[i] * arr[j] > arr[i] + arr[j] // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] function countPairs($arr, $n) { $count = 0; for ($i = 0; $i < $n - 1; $i++) { for ($j = $i + 1; $j < $n; $j++) { // If condition is satisfied if ($arr[$i] * $arr[$j] > $arr[$i] + $arr[$j]) $count++; } } return $count; } // Driver code $arr = array( 5, 0, 3, 1, 2 ); $n = sizeof($arr) ; echo countPairs($arr, $n); // This code is contributed by Ryuga ?> |
3
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++
// C++ program to count pairs (i, j) // such that arr[i] * arr[j] > arr[i] + arr[j] #include <bits/stdc++.h> using namespace std; // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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; } // Driver code int main() { int arr[] = { 5, 0, 3, 1, 2 }; int n = sizeof(arr) / sizeof(arr[0]); cout << countPairs(arr, n); return 0; } |
Java
// Java program to count pairs (i, j) // such that arr[i] * arr[j] > arr[i] + arr[j] class GFG { // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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; } // Driver code public static void main(String[] args) { int arr[] = {5, 0, 3, 1, 2}; int n = arr.length; System.out.println(countPairs(arr, n)); } } // This code is contributed by // 29AjayKumar |
Python3
# Python3 program to count pairs(i,j) # such that arr[i]*arr[j]>arr[i]+arr[j] import math as mt # function to return the count of pairs # such that arr[i]*arr[j]>arr[i]+arr[j] 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 # Driver code arr = [5, 0, 3, 1, 2] n = len(arr) print(countPairs(arr, n)) # This code is contributed # by Mohit Kumar |
C#
// C# program to count pairs (i, j) such // that arr[i] * arr[j] > arr[i] + arr[j] using System; class GFG { // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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; } // Driver code public static void Main() { int[] arr = {5, 0, 3, 1, 2}; int n = arr.Length; Console.WriteLine(countPairs(arr, n)); } } // This code is contributed by // Mukul Singh |
PHP
<?php // PHP program to count pairs (i, j) such // that arr[i] * arr[j] > arr[i] + arr[j] // Function to return the count of pairs // such that arr[i] * arr[j] > arr[i] + arr[j] 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; } // Driver code $arr = array( 5, 0, 3, 1, 2 ); $n = sizeof($arr); echo countPairs($arr, $n); // This code is contributed // by Akanksha Rai ?> |
3
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.
Recommended Posts:
- Count pairs with Bitwise-AND as even number
- Count pairs with Bitwise XOR as EVEN number
- Count the number of pairs that have column sum greater than row sum
- Number of pairs such that path between pairs has the two vertices A and B
- Count of even and odd power pairs in an Array
- Count of pairs violating BST property
- Count pairs in array whose sum is divisible by K
- Count pairs in an array which have at least one digit common
- Count pairs in an array such that at least one element is prime
- Count unequal element pairs from the given Array
- Count of pairs from Array with sum equal to twice their bitwise AND
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count possible combinations of pairs with adjacent elements from first N numbers
- Max count of unique ratio/fraction pairs in given arrays
- Count pairs from two linked lists whose product is equal to a given value
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Number of pairs with Bitwise OR as Odd number
- Find the number of pairs (a, b) such that a % b = K
- Number of special pairs possible from the given two numbers
- Number of pairs with Pandigital Concatenation
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

