📌  相关文章
📜  计数对的索引乘积等于这些索引上存在的元素乘积

📅  最后修改于: 2021-04-17 19:17:21             🧑  作者: Mango

给定由N个整数组成的数组arr [] ,任务是计算索引乘积等于该索引处元素乘积的不同数组元素对的数量。对(x,y)(y,x)被视为相同的对。

例子:

方法:可以通过从给定数组生成所有可能的元素对(arr [i],arr [j])来解决给定的问题并且如果ij的乘积等于数组元素arr [i的乘积]arr [j] ,然后增加此类对的计数。检查完所有不同的对后,将总计打印为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
3

时间复杂度: O(N 2 )
辅助空间: O(1)