Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]

Given an array arr[] of length N, count the number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j] and 0 <= i < j <= N. It is also given that elements of the array can be any positive integers including zero.

Examples:

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

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

Simple solution:
We can generate all possible pairs of the array and count those pairs which satisfy the given condition.



Below is the implementation of above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(i, j)
// 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++) {
  
            // Increment count if condition satisfy
            if (arr[i] * arr[j] == arr[i] + arr[j])
                count++;
        }
    }
  
    // Return count of pairs
    return count;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 0, 3, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Get and print count of pairs
    cout << countPairs(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(i, j)
    // 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++) {
  
                // Increment count if condition satisfy
                if (arr[i] * arr[j] == arr[i] + arr[j])
                    count++;
            }
        }
  
        // Return count of pairs
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int arr[] = { 2, 0, 3, 2, 0 };
        int n = arr.length;
  
        // Get and print count of pairs
        System.out.println(countPairs(arr, n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count pairs (i, j) 
# such that arr[i] * arr[j] = arr[i] + arr[j] 
  
# Function to return the count of pairs(i, j) 
# such that arr[i] * arr[j] = arr[i] + arr[j] 
def countPairs(arr, n) : 
  
    count = 0
  
    for i in range(n - 1) :
        for j in range(i + 1, n) :
  
            # Increment count if condition satisfy 
            if (arr[i] * arr[j] == arr[i] + arr[j]) :
                count += 1
  
    # Return count of pairs 
    return count; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 2, 0, 3, 2, 0 ]; 
    n = len(arr); 
  
    # Get and print count of pairs 
    print(countPairs(arr, n)); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(i, j)
    // 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++) {
  
                // Increment count if condition satisfy
                if (arr[i] * arr[j] == arr[i] + arr[j])
                    count++;
            }
        }
  
        // Return count of pairs
        return count;
    }
  
    // Driver code
    public static void Main(string[] args)
    {
  
        int[] arr = { 2, 0, 3, 2, 0 };
        int n = arr.Length;
  
        // Get and print count of pairs
        Console.WriteLine(countPairs(arr, n));
    }
}

chevron_right


Output:

2

Time Complexity: O(n2)

Efficient Solution:
Taking arr[i] as x and arr[j] as y, we can rewrite the given condition as the following equation.

xy = x + y
xy - x - y = 0
xy - x - y + 1 = 1
x(y - 1) -(y - 1) = 1 
(x - 1)(y - 1) = 1

Case 1:
x - 1 = 1 i.e x = 2
y - 1 = 1 i.e y = 2

Case 2:
x - 1 = -1 i.e x = 0
y - 1 = -1 i.e y = 0

So, now we know that the condition arr[i] * arr[j] = arr[i] + arr[j] will satisfy only if either arr[i] = arr[j] = 0 or arr[i] = arr[j] = 2.
All we need to do is to count the occurrence of 2’s and 0’s. We can then get the number of pairs using formula

(count * (count - 1)) / 2

Below is the implementation of above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
long countPairs(int arr[], int n)
{
  
    int countZero = 0;
    int countTwo = 0;
  
    // Count number of 0's and 2's in the array
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0)
            countZero++;
  
        else if (arr[i] == 2)
            countTwo++;
    }
  
    // Total pairs due to occurence of 0's
    long pair0 = (countZero * (countZero - 1)) / 2;
  
    // Total pairs due to occurence of 2's
    long pair2 = (countTwo * (countTwo - 1)) / 2;
  
    // Return count of all pairs
    return pair0 + pair2;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 0, 3, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Get and print count of pairs
    cout << countPairs(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(i, j)
    // such that arr[i] * arr[j] = arr[i] + arr[j]
    static long countPairs(int arr[], int n)
    {
  
        int countZero = 0;
        int countTwo = 0;
  
        // Count number of 0's and 2's in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                countZero++;
  
            else if (arr[i] == 2)
                countTwo++;
        }
  
        // Total pairs due to occurence of 0's
        long pair0 = (countZero * (countZero - 1)) / 2;
  
        // Total pairs due to occurence of 2's
        long pair2 = (countTwo * (countTwo - 1)) / 2;
  
        // Return count of all pairs
        return pair0 + pair2;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int arr[] = { 2, 0, 3, 2, 0 };
        int n = arr.length;
  
        // Get and print count of pairs
        System.out.println(countPairs(arr, n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count pairs (i, j) 
# such that arr[i] * arr[j] = arr[i] + arr[j] 
  
# Function to return the count of pairs(i, j) 
# such that arr[i] * arr[j] = arr[i] + arr[j] 
def countPairs(arr, n): 
  
    countZero = 0
    countTwo = 0
  
    # Count number of 0's and 2's in the array 
    for i in range(n) : 
        if (arr[i] == 0) :
            countZero += 1
  
        elif (arr[i] == 2) :
            countTwo += 1;
  
    # Total pairs due to occurence of 0's 
    pair0 = (countZero * (countZero - 1)) // 2
  
    # Total pairs due to occurence of 2's 
    pair2 = (countTwo * (countTwo - 1)) // 2
  
    # Return count of all pairs 
    return pair0 + pair2; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 2, 0, 3, 2, 0 ]; 
    n = len(arr); 
  
    # Get and print count of pairs 
    print(countPairs(arr, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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(i, j)
    // such that arr[i] * arr[j] = arr[i] + arr[j]
    static long countPairs(int[] arr, int n)
    {
  
        int countZero = 0;
        int countTwo = 0;
  
        // Count number of 0's and 2's in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                countZero++;
  
            else if (arr[i] == 2)
                countTwo++;
        }
  
        // Total pairs due to occurence of 0's
        long pair0 = (countZero * (countZero - 1)) / 2;
  
        // Total pairs due to occurence of 2's
        long pair2 = (countTwo * (countTwo - 1)) / 2;
  
        // Return count of all pairs
        return pair0 + pair2;
    }
  
    // Driver code
    public static void Main(string[] args)
    {
  
        int[] arr = { 2, 0, 3, 2, 0 };
        int n = arr.Length;
  
        // Get and print count of pairs
        Console.WriteLine(countPairs(arr, n));
    }
}

chevron_right


Output:

2

Time Complexity: O(n)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




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.



Improved By : AnkitRai01