Count of pairs having even and odd LCM from an array | C ++ program for the above approach ; Function to find count of distinct pairs having even LCM and odd LCM ; Store the total number of pairs ; Stores the count of odd numbers in the array ; Traverse the array arr [ ] ; Update the count of pairs with odd LCM ; Print the count of required pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LCMPairs ( int arr [ ] , int N ) { int total_pairs = ( N * ( N - 1 ) ) / 2 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; } odd = ( odd * ( odd - 1 ) ) / 2 ; cout << " Even ▁ = ▁ " << total_pairs - odd << " , ▁ Odd ▁ = ▁ " << odd ; } int main ( ) { int arr [ ] = { 3 , 6 , 5 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; LCMPairs ( arr , N ) ; return 0 ; }