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

📅  最后修改于: 2021-09-05 11:51:00             🧑  作者: 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


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live