📌  相关文章
📜  对数组中的对进行计数,以使LCM(arr [i],arr [j])> min(arr [i],arr [j])

📅  最后修改于: 2021-04-22 07:42:12             🧑  作者: Mango

给定一个数组arr [] ,任务是从该数组中查找对数,以使LCM(arr [i],arr [j])> min(arr [i],arr [j])
注意:(arr [i],arr [j])(arr [j],arr [i])被认为是相同的,并且仅被计数一次。

例子:

方法:观察到只有arr [i] = arr [j]形式的对(arr [i],arr [j])不满足给定条件。因此,现在问题被简化为找到对数(arr [i],arr [j]),从而使arr [i]!= arr [j]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of valid pairs
int count_pairs(int n, int a[])
{
    // Store frequencies of array elements
    unordered_map frequency;
    for (int i = 0; i < n; i++) {
        frequency[a[i]]++;
    }
  
    int count = 0;
  
    // Count of pairs (arr[i], arr[j]) 
    // where arr[i] = arr[j]
    for (auto x : frequency) {
        int f = x.second;
        count += f * (f - 1) / 2;
    }
  
    // Count of pairs (arr[i], arr[j]) where
    // arr[i] != arr[j], i.e. Total pairs - pairs 
    // where arr[i] = arr[j]
    return ((n * (n - 1)) / 2) - count;
}
  
// Driver Code
int main()
{
    int arr[] = { 2, 4, 5, 2, 5, 7, 2, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << count_pairs(n, arr);
    return 0;
}


Java
// Java implementation of the approach
import java.util.HashMap;
import java.util.Map;
  
class GfG
{
  
    // Function to return the count of valid pairs 
    static int count_pairs(int n, int a[]) 
    { 
        // Store frequencies of array elements 
        HashMap frequency = new HashMap<>(); 
        for (int i = 0; i < n; i++)
        { 
              
            if (!frequency.containsKey(a[i]))
                frequency.put(a[i], 0);
            frequency.put(a[i], frequency.get(a[i])+1); 
        } 
      
        int count = 0; 
      
        // Count of pairs (arr[i], arr[j]) 
        // where arr[i] = arr[j] 
        for (Map.Entry x: frequency.entrySet())
        { 
            int f = x.getValue(); 
            count += f * (f - 1) / 2; 
        } 
      
        // Count of pairs (arr[i], arr[j]) where 
        // arr[i] != arr[j], i.e. Total pairs - pairs 
        // where arr[i] = arr[j] 
        return ((n * (n - 1)) / 2) - count; 
    }
  
    // Driver code
    public static void main(String []args)
    {
          
        int arr[] = { 2, 4, 5, 2, 5, 7, 2, 8 }; 
        int n = arr.length; 
        System.out.println(count_pairs(n, arr));
    }
}
      
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of the approach 
  
# Function to return the count 
# of valid pairs 
def count_pairs(n, a) : 
  
    # Store frequencies of array elements 
    frequency = dict.fromkeys(a, 0) 
    for i in range(n) :
        frequency[a[i]] += 1
  
    count = 0
  
    # Count of pairs (arr[i], arr[j]) 
    # where arr[i] = arr[j] 
    for f in frequency.values() : 
        count += f * (f - 1) // 2
      
    # Count of pairs (arr[i], arr[j]) where 
    # arr[i] != arr[j], i.e. Total pairs - pairs 
    # where arr[i] = arr[j] 
    return ((n * (n - 1)) // 2) - count
  
# Driver Code 
if __name__ == "__main__" :
      
    arr = [ 2, 4, 5, 2, 
            5, 7, 2, 8 ] 
    n = len(arr)
    print(count_pairs(n, arr))
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
class GfG
{
  
    // Function to return the count of valid pairs 
    static int count_pairs(int n, int []arr) 
    { 
        // Store frequencies of array elements 
        Dictionary mp = new Dictionary();
        for (int i = 0 ; i < n; i++)
        {
            if(mp.ContainsKey(arr[i]))
            {
                var val = mp[arr[i]];
                mp.Remove(arr[i]);
                mp.Add(arr[i], val + 1); 
            }
            else
            {
                mp.Add(arr[i], 1);
            }
        }
        int count = 0; 
      
        // Count of pairs (arr[i], arr[j]) 
        // where arr[i] = arr[j] 
        foreach(KeyValuePair x in mp)
        { 
            int f = x.Value; 
            count += f * (f - 1) / 2; 
        } 
      
        // Count of pairs (arr[i], arr[j]) where 
        // arr[i] != arr[j], i.e. Total pairs - pairs 
        // where arr[i] = arr[j] 
        return ((n * (n - 1)) / 2) - count; 
    }
  
    // Driver code
    public static void Main(String []args)
    {
          
        int []arr = { 2, 4, 5, 2, 5, 7, 2, 8 }; 
        int n = arr.Length; 
        Console.WriteLine(count_pairs(n, arr));
    }
}
  
// This code is contributed by Rajput-Ji


输出:
24