Given an array A[] of length N, the task is to find the total number of distinct pairs (i, j) of indices where 1 ? i < j ? N such that the greatest common divisor(gcd) and least common multiple(lcm) of these elements are equal.
Examples:
Input: A[] = {2, 5, 5, 5, 6}
Output: 3
?Explanation: Here pair (i, j) are: (2, 3), (3, 4), and (2, 4).
To elaborate, gcd(A2, A3) = lcm(A2, A3) = 5.Input: A[] = {22, 22, 38, 38}
Output: 2
Approach: The problem can be solved based on the following observation:
Observations:
- The observation to be made here is as follows: gcd(Ai, Aj) = lcm(Ai, Aj)? Ai = Aj
- So, the problem reduces to simply counting the number of pairs of equal elements in A.
- Build a frequency map of the elements of A (using map/unordered_map in C++, TreeMap/Hashmap in Java, or dict in python) and then iterate across its elements.
- If an element x has a frequency of fx, then it contributes fx?(fx?1)/2 pairs to the answer, so sum this value across all x.
Follow the below steps to solve the problem:
- Declare a hash map.
- Start iterating over the entire array
- If the element is present in the map, then increase the value of frequency by 1.
- Otherwise, insert that element into the map.
- After that start traversing the map and get the value(frequency of element).
- If an element x has a frequency of fx, then it contributes fx?(fx ? 1) / 2 pairs to the answer, so sum this value across all x.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; long pairCount( int arr[], int size) { // Function to find number of // distinct pair unordered_map< int , int > freqMap; for ( int i = 0; i < size; i++) { freqMap[arr[i]]++; } long ans = 0; for ( auto it : freqMap) { ans += ( long )it.second * ( long )(it.second - 1) / 2; } return ans; } // Driver code int main() { // Function call int A[] = { 2, 5, 5, 5, 6 }; int size = sizeof (A) / sizeof (A[0]); cout << pairCount(A, size); } // This code is contributed by aarohi2616. |
Java
// Java code to implement the approach import java.io.*; import java.util.*; public class GFG { // Function to find number of // distinct pair public static long pairCount( int a[], int n) { Map<Integer, Integer> mp = new HashMap<>(); for ( int i = 0 ; i < n; i++) { if (mp.containsKey(a[i])) { mp.put(a[i], mp.get(a[i]) + 1 ); } else { mp.put(a[i], 1 ); } } long ans = 0 ; for ( int i : mp.values()) { ans += ( long )i * ( long )(i - 1 ) / 2 ; } return ans; } // Driver code public static void main(String[] args) { int A[] = { 2 , 5 , 5 , 5 , 6 }; int N = A.length; // Function call System.out.println(pairCount(A, N)); } } |
C#
// C# code to implement the approach using System; using System.Collections.Generic; public class GFG { // Function to find number of distinct pair public static long pairCount( int [] a, int n) { Dictionary< int , int > mp = new Dictionary< int , int >(); for ( int i = 0; i < n; i++) { if (mp.ContainsKey(a[i])) { mp[a[i]] += 1; } else { mp.Add(a[i], 1); } } long ans = 0; Dictionary< int , int >.ValueCollection valueColl = mp.Values; foreach ( int i in valueColl) { ans += ( long )i * ( long )(i - 1) / 2; } return ans; } static public void Main() { // Code int [] A = { 2, 5, 5, 5, 6 }; int N = A.Length; // Function call Console.WriteLine(pairCount(A, N)); } } // This code is contributed by lokeshmvs21. |
Python3
class GFG : # Function to find number of # distinct pair @staticmethod def pairCount( a, n) : mp = dict () i = 0 while (i < n) : if ((a[i] in mp.keys())) : mp[a[i]] = mp.get(a[i]) + 1 else : mp[a[i]] = 1 i + = 1 ans = 0 for i in mp.values() : ans + = int (i) * int ((i - 1 )) / 2 return ans # Driver code @staticmethod def main( args) : A = [ 2 , 5 , 5 , 5 , 6 ] N = len (A) # Function call print (GFG.pairCount(A, N)) if __name__ = = "__main__" : GFG.main([]) # This code is contributed by aadityaburujwale. |
Javascript
class GFG { // Function to find number of // distinct pair static pairCount(a, n) { var mp = new Map(); for (i; i < n; i++) { if (mp.has(a[i])) { mp.set(a[i],mp.get(a[i]) + 1); } else { mp.set(a[i],1); } } var ans = 0; for ( const i of mp.values()) { ans += parseInt(i) * parseInt((i - 1)) / 2; } return ans; } // Driver code static main(args) { var A = [2, 5, 5, 5, 6]; var N = A.length; // Function call console.log(GFG.pairCount(A, N)); } } GFG.main([]); // This code is contributed by aadityaburujwale. |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!