Number of pairs in an array having sum equal to product

Given an array arr[], the task is to find the number of pairs (arr[i], arr[j]) in the array such that arr[i] + arr[j] = arr[i] * arr[j]

Examples:

Input: arr[] = {2, 2, 3, 4, 6}
Output: 1
(2, 2) is the only possible pair as (2 + 2) = (2 * 2) = 4.

Input: arr[] = {1, 2, 3, 4, 5}
Output: 0

Approach: The only possible pairs of integers that will satisfy the given conditions are (0, 0) and (2, 2). So the task now is to count the number of 0s and 2s in the array and store them in cnt0 and cnt2 respectively and then the required count will be (cnt0 * (cnt0 – 1)) / 2 + (cnt2 * (cnt2 – 1)) / 2.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count
// of the required pairs
int sumEqualProduct(int a[], int n)
{
    int zero = 0, two = 0;
  
    // Find the count of 0s
    // and 2s in the array
    for (int i = 0; i < n; i++) {
        if (a[i] == 0) {
            zero++;
        }
        if (a[i] == 2) {
            two++;
        }
    }
  
    // Find the count of required pairs
    int cnt = (zero * (zero - 1)) / 2
              + (two * (two - 1)) / 2;
  
    // Return the count
    return cnt;
}
  
// Driver code
int main()
{
    int a[] = { 2, 2, 3, 4, 2, 6 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << sumEqualProduct(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to return the count
// of the required pairs
static int sumEqualProduct(int a[], int n)
{
    int zero = 0, two = 0;
  
    // Find the count of 0s
    // and 2s in the array
    for (int i = 0; i < n; i++)
    {
        if (a[i] == 0)
        {
            zero++;
        }
        if (a[i] == 2
        {
            two++;
        }
    }
  
    // Find the count of required pairs
    int cnt = (zero * (zero - 1)) / 2
                (two * (two - 1)) / 2;
  
    // Return the count
    return cnt;
}
  
// Driver code
public static void main(String[] args)
{
    int a[] = { 2, 2, 3, 4, 2, 6 };
    int n = a.length;
  
    System.out.print(sumEqualProduct(a, n));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach 
  
# Function to return the count 
# of the required pairs 
def sumEqualProduct(a, n):
    zero = 0
    two = 0
      
    # Find the count of 0s 
    # and 2s in the array 
    for i in range(n):
        if a[i] == 0:
            zero += 1
        if a[i] == 2:
            two += 1
              
    # Find the count of required pairs
    cnt = (zero * (zero - 1)) // 2 + \
            (two * (two - 1)) // 2
      
    # Return the count
    return cnt
      
# Driver code 
a = [ 2, 2, 3, 4, 2, 6
n = len(a) 
  
print(sumEqualProduct(a, n))
  
# This code is contributed by Ankit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the count
// of the required pairs
static int sumEqualProduct(int []a, int n)
{
    int zero = 0, two = 0;
  
    // Find the count of 0s
    // and 2s in the array
    for (int i = 0; i < n; i++)
    {
        if (a[i] == 0)
        {
            zero++;
        }
        if (a[i] == 2) 
        {
            two++;
        }
    }
  
    // Find the count of required pairs
    int cnt = (zero * (zero - 1)) / 2 + 
               (two * (two - 1)) / 2;
  
    // Return the count
    return cnt;
}
  
// Driver code
public static void Main(String[] args)
{
    int []a = { 2, 2, 3, 4, 2, 6 };
    int n = a.Length;
  
    Console.Write(sumEqualProduct(a, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

3

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.