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 the above approach:
CPP
// 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 codeint 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;} |
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(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));    }} |
Python3
# 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 |
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(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));    }} |
Javascript
<script>// Function to return the count of pairs(i, j)// such that arr[i] * arr[j] = arr[i] + arr[j]function countPairs(arr, n){let count = 0;Â
for (let i = 0; i < n - 1; i++) {for (let j = i + 1; j < n; j++) {Â
Â
// Increment count if condition satisfyif (arr[i] * arr[j] == arr[i] + arr[j])count++;}}Â
// Return count of pairsreturn count;}Â
Â
let arr = [ 2, 0, 3, 2, 0 ];let n = arr.length;Â
document.write(countPairs(arr, n));// This code is contributed by khatriharsh281</script> |
2
Time Complexity: O(n2)
Auxiliary Space: O(12)
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 the above approach:
CPP
// 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 occurrence of 0's    long pair0 = (countZero * (countZero - 1)) / 2;Â
    // Total pairs due to occurrence of 2's    long pair2 = (countTwo * (countTwo - 1)) / 2;Â
    // Return count of all pairs    return pair0 + pair2;}Â
// Driver codeint 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;} |
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(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 occurrence of 0's        long pair0 = (countZero * (countZero - 1)) / 2;Â
        // Total pairs due to occurrence 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));    }} |
Python3
# 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 occurrence of 0's     pair0 = (countZero * (countZero - 1)) // 2; Â
    # Total pairs due to occurrence 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 |
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(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 occurrence of 0's        long pair0 = (countZero * (countZero - 1)) / 2;Â
        // Total pairs due to occurrence 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));    }} |
2
Time Complexity: O(n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
<!–
–>














Please Login to comment…