Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++

C++Server Side ProgrammingProgramming

We are given an array of positive integers. The goal is to find the count of pairs of elements of arr[] such that condition LCM( arr[i], arr[j] ) > minimum of ( arr[i], arr[j] ). That is, the lowest common multiple of elements in a pair is greater than the minimum of both.

Note − pair ( arr[i], arr[j] ) is the same as ( arr[j],arr[i] ). Don’t count it twice.

Let us understand with examples.

Input − arr[] = [ 1,5,4,2 ]

Output − Count of pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) are − 6

Explanation − Total 6 pairs are −

Pair 1 (1,5) LCM is 5 > 1
Pair 2 (1,4) LCM is 4 > 1
Pair 3 (1,2) LCM is 2 > 1
Pair 4 (5,4) LCM is 20 > 4
Pair 5 (5,2) LCM is 10 > 2
Pair 6 (4,2) LCM is 4 > 2

Input − arr[] = [ 3,3,6 ]

Output − Count of pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) are − 2

Explanation − Total 2 pairs are −

Pair 1 (3,6) LCM is 6 > 3
Pair 2 (3,6) LCM is 6 > 3

Approach used in the below program is as follows

The above condition will be false only where arr[i]==arr[j]. Pairs will be formed between unique elements. Take an unordered map containing frequencies of unique elements of the array. Now to remove all similar element pairs. Calculate for each frequency the maximum pairs as ( freq * (freq-1)/2 ). Add all such calculations to count.

Total array has size elements then maximum pairs=size(size-1)/2.

Result would be pairs-count. ( All pairs - pairs with the same element ).

Take an array of integers.

  • Take an array of integers.

  • Function conditional_pair(int arr[], int size) takes the array and its size and returns the count of pairs such that the condition is met.

  • Take the initial count as 0.

  • Take unordered_map<int, int> um for elements of arr[] and their frequencies.

  • Traverse map using for and for each frequency temp = it.second; (it=iterator) Add temp*(temp-1)/2 to count. All possible pairs for temp number of elements.

  • Now count has all pairs of the same elements that will not be considered in our case.

  • Calculate total possible pairs for array elements as temp=size*(size-1)/2.

  • Update count as count - temp.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int conditional_pair(int arr[], int size){
   int count = 0;
   unordered_map<int, int> um;
   for (int i = 0; i < size; i++){
      um[arr[i]]++;
   }
   for (auto it : um){
      int temp = it.second;
      count = count + temp * (temp - 1) / 2;
   }
   int temp = (size * (size - 1)) / 2;
   count = temp - count;
   return count;
}
int main(){
   int arr[] = { 4, 1, 7, 3, 2};
   int size = sizeof(arr) / sizeof(arr[0]);
   cout<<"Count of pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) are:
"<<conditional_pair(arr, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) are: 10
raja
Published on 02-Dec-2020 11:35:00
Advertisements