给定一个由N 个整数组成的数组arr[] ,任务是计算具有索引乘积等于该索引处元素乘积的不同数组元素对的数量。对(x, y)和(y, x)被认为是相同的对。
例子:
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
方法:可以通过从给定数组中生成所有可能的不同元素对(arr[i], arr[j])来解决给定的问题,如果i和j的乘积等于数组元素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