Count distinct pair of indices in Array whose GCD and LCM are same
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++
#include <bits/stdc++.h>
using namespace std;
long pairCount( int arr[], int size)
{
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;
}
int main()
{
int A[] = { 2, 5, 5, 5, 6 };
int size = sizeof (A) / sizeof (A[0]);
cout << pairCount(A, size);
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
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;
}
public static void main(String[] args)
{
int A[] = { 2 , 5 , 5 , 5 , 6 };
int N = A.length;
System.out.println(pairCount(A, N));
}
}
|
C#
using System;
using System.Collections.Generic;
public class GFG {
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()
{
int [] A = { 2, 5, 5, 5, 6 };
int N = A.Length;
Console.WriteLine(pairCount(A, N));
}
}
|
Python3
class GFG :
@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
@staticmethod
def main( args) :
A = [ 2 , 5 , 5 , 5 , 6 ]
N = len (A)
print (GFG.pairCount(A, N))
if __name__ = = "__main__" :
GFG.main([])
|
Javascript
class GFG
{
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;
}
static main(args)
{
var A = [2, 5, 5, 5, 6];
var N = A.length;
console.log(GFG.pairCount(A, N));
}
}
GFG.main([]);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
30 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...