Open In App

Count pairs with product of indices equal to the product of elements present at those indices

Last Updated : 29 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] consisting of N integers, the task is to count the number of distinct pairs of array elements having the product of indices equals the product of elements at that indices. The pairs (x, y) and (y, x) are considered as the same pairs.

Examples:

Input: arr[] = {1, 0, 3, 2, 6}
Output: 3
Explanation: All possible pairs satisfying the given criteria are:

  • (0, 1): Product of indices = 1 * 0 = 0. Product of elements = 1 * 0 = 0.
  • (2, 3): Product of indices = 2 * 3 = 6. Product of elements = 3 * 2 = 6.
  • (3, 4): Product of indices = 3 * 4 = 12. Product of elements = 2 * 6 = 12.

Therefore, the total count of pairs is 3.

Input: arr[] = {4, -1, 2, 6, 2, 10}
Output: 2

Approach: The given problem can be solved by generating all possible distinct pairs of elements (arr[i], arr[j]) from the given array and if the product of i and j is equal to the product of array elements arr[i] and arr[j], then increment the count of such pairs. After checking for all the distinct pairs, print the total count as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to count the number of pairs
// having product of indices equal to the
// product of elements at that indices
int CountPairs(int arr[], int n)
{
    // Stores the count of valid pairs
    int count = 0;
 
    // Generate all possible pairs
    for (int i = 0; i < n - 1; i++) {
 
        for (int j = i + 1; j < n; j++) {
 
            // If the condition is satisfied
            if ((i * j) == (arr[i] * arr[j]))
 
                // Increment the count
                count++;
        }
    }
 
    // Return the total count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 3, 2, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << CountPairs(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the number of pairs
// having product of indices equal to the
// product of elements at that indices
static int CountPairs(int[] arr, int n)
{
     
    // Stores the count of valid pairs
    int count = 0;
 
    // Generate all possible pairs
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
             
            // If the condition is satisfied
            if ((i * j) == (arr[i] * arr[j]))
             
                // Increment the count
                count++;
        }
    }
 
    // Return the total count
    return count;
}
 
// Driver Code
public static void main (String[] args)
{
    int[] arr = { 1, 0, 3, 2, 6 };
    int N = arr.length;
 
    System.out.println(CountPairs(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python3 program for the above approach
 
# Function to count the number of pairs
# having product of indices equal to the
# product of elements at that indices
def CountPairs(arr, n):
 
    # Stores the count of valid pairs
    count = 0
 
    # Generate all possible pairs
    for i in range(n - 1):
        for j in range(i + 1, n):
 
            # If the condition is satisfied
            if ((i * j) == (arr[i] * arr[j])):
 
                # Increment the count
                count += 1
 
    # Return the total count
    return count
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 1, 0, 3, 2, 6 ]
    N = len(arr)
 
    print(CountPairs(arr, N))
 
# This code is contributed by AnkThon


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to count the number of pairs
// having product of indices equal to the
// product of elements at that indices
static int CountPairs(int[] arr, int n)
{
     
    // Stores the count of valid pairs
    int count = 0;
 
    // Generate all possible pairs
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
             
            // If the condition is satisfied
            if ((i * j) == (arr[i] * arr[j]))
             
                // Increment the count
                count++;
        }
    }
 
    // Return the total count
    return count;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 0, 3, 2, 6 };
    int N = arr.Length;
 
    Console.Write(CountPairs(arr, N));
}
}
 
// This code is contributed by ukasp


Javascript




<script>
// javascript program for the above approach
 
    // Function to count the number of pairs
    // having product of indices equal to the
    // product of elements at that indices
    function CountPairs(arr , n) {
 
        // Stores the count of valid pairs
        var count = 0;
 
        // Generate all possible pairs
        for (i = 0; i < n - 1; i++) {
            for (j = i + 1; j < n; j++) {
 
                // If the condition is satisfied
                if ((i * j) == (arr[i] * arr[j]))
 
                    // Increment the count
                    count++;
            }
        }
 
        // Return the total count
        return count;
    }
 
    // Driver Code
     
        var arr = [ 1, 0, 3, 2, 6 ];
        var N = arr.length;
 
        document.write(CountPairs(arr, N));
// This code contributed by Rajput-Ji
</script>


Output: 

3

 

Time Complexity: O(N2)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads