📌  相关文章
📜  从数组中计算对 (i, j) 的数量,使得 arr[i] * j = arr[j] * i

📅  最后修改于: 2021-09-04 11:38:48             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是计算数组中可能的对(i, j)的数量,使得arr[j] * i = arr[i] * j ,其中1 ≤ i < j ≤ N。

例子:

朴素的方法:解决问题的最简单的方法是从数组中生成所有可能的对,并检查每一对,是否满足给定的条件。增加满足条件的对的计数。最后,打印所有这些对的计数。

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

高效的方法:为了优化上述方法,该想法基于给定方程arr[i] * j = arr[j] * i的重新排列到arr[i] / i = arr[j] / j
请按照以下步骤解决问题:

  • 初始化一个变量,比如count ,以存储满足给定条件的对的总数。
  • 初始化一个无序映射,比如mp,以计算值arr[i] / i的频率。
  • 遍历数组arr[]并更新 Map 中arr[i]/i的频率。
  • 打印计数作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count pairs from an
// array satisfying given conditions
void countPairs(int arr[], int N)
{
    // Stores the total
    // count of pairs
    int count = 0;
 
    // Stores count of a[i] / i
    unordered_map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        double val = 1.0 * arr[i];
        double idx = 1.0 * (i + 1);
 
        // Updating count
        count += mp[val / idx];
 
        // Update frequency
        // in the Map
        mp[val / idx]++;
    }
 
    // Print count of pairs
    cout << count;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 3, 5, 6, 5 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to count pairs
    // satisfying given conditions
    countPairs(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
   
// Function to count pairs from an
// array satisfying given conditions
static void countPairs(int []arr, int N)
{
     
    // Stores the total
    // count of pairs
    int count = 0;
 
    // Stores count of a[i]/i
    Map  mp
            = new HashMap();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        Double val = 1.0 * arr[i];
        Double idx = 1.0 * (i + 1);
 
        // Updating count
        if (mp.containsKey(val / idx))
            count += mp.get(val/idx);
 
        // Update frequency
        // in the Map
        if (mp.containsKey(val / idx))
            mp.put(val / idx, mp.getOrDefault(val / idx, 0) + 1);
        else
            mp.put(val/idx, 1);
    }
 
    // Print count of pairs
    System.out.print(count);
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given array
    int []arr = { 1, 3, 5, 6, 5 };
 
    // Size of the array
    int N = arr.length;
 
    // Function call to count pairs
    // satisfying given conditions
    countPairs(arr, N);
}
}
 
// This code is contributed by ipg2016107.


Python3
# Python3 program for the above approach
from collections import defaultdict
 
# Function to count pairs from an
# array satisfying given conditions
def countPairs(arr, N):
 
    # Stores the total
    # count of pairs
    count = 0
 
    # Stores count of a[i] / i
    mp = defaultdict(int)
 
    # Traverse the array
    for i in range(N):
        val = 1.0 * arr[i]
        idx = 1.0 * (i + 1)
 
        # Updating count
        count += mp[val / idx]
 
        # Update frequency
        # in the Map
        mp[val / idx] += 1
 
    # Print count of pairs
    print(count)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array
    arr = [1, 3, 5, 6, 5]
 
    # Size of the array
    N = len(arr)
 
    # Function call to count pairs
    # satisfying given conditions
    countPairs(arr, N)
 
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to count pairs from an
// array satisfying given conditions
static void countPairs(int []arr, int N)
{
     
    // Stores the total
    // count of pairs
    int count = 0;
 
    // Stores count of a[i]/i
    Dictionary mp = new Dictionary();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        double val = 1.0 * arr[i];
        double idx = 1.0 * (i + 1);
 
        // Updating count
        if (mp.ContainsKey(val / idx))
            count += mp[val/idx];
 
        // Update frequency
        // in the Map
        if (mp.ContainsKey(val / idx))
            mp[val / idx]++;
        else
            mp[val/idx] = 1;
    }
 
    // Print count of pairs
    Console.WriteLine(count);
}
 
// Driver Code
public static void Main()
{
     
    // Given array
    int []arr = { 1, 3, 5, 6, 5 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function call to count pairs
    // satisfying given conditions
    countPairs(arr, N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
2

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

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