- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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

- Related Articles
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Rearrange an array such that arr[i] = i in C++
- Maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1] - when elements are from 1 to n in C++