Open In App

Count of pairs having even and odd LCM from an array

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size N, the task is to count the number of pairs having even LCM and odd LCM.

Examples:

Input: arr[] = {3, 6, 5, 4}
Output: Even = 5, Odd = 1
Explanation: LCM of (3, 6) is 6, LCM of (3, 5) is 15, LCM of (3, 4) is 12, LCM of (6, 5) is 30, LCM of (6, 4) is 12, LCM of (5, 4) is 20.
Pairs having even LCM are (3, 6), (3, 4), (6, 5), (6, 4) and (5, 4).
Only pair having odd LCM is (3, 5).

Input: arr[] = {4, 7, 2, 12}
Output: Even = 6, Odd = 0
Explanation: Pairs having even LCM = (4, 7), (4, 2), (4, 12), (7, 2), (7, 12), (2, 12).
No pair has odd LCM.

Naive Approach: The simplest approach is to generate all possible pairs to get all distinct pairs and for each pair, calculate their LCM. If their LCM is even, then increase the count of even. Otherwise, increase the count of odd. Finally, print their count separately. 
Time Complexity: O((N2)*log(M)), where M is the smallest element in the array 
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is based on the fact that the LCM of 2 numbers is odd if and only if both the numbers are odd. Thus, find the total odd pairs in the array and to get a count of pairs with even LCM, subtract the count of odd pairs from the total number of possible pairs.

Follow the steps below to solve the problem:

  • Store the total count of pairs in a variable, say totalPairs. Initialize totalPairs as (N*(N – 1))/2.
  • Store the count of odd elements in the array in a variable, say cnt.
  • Store the count of pairs consisting of odd numbers only, in a variable, say odd. Therefore, odd = (cnt*(cnt – 1))/2.
  • After completing the above steps, print odd as the value of the count of pairs with odd LCM. Print (totalPairs – odd) as the count of pairs having even LCM.

Below is the implementation of the above approach.

C++




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


Java




// Java program to implement
// the above approach
import java.util.*;
class GFG
{
  
// Function to find count of distinct
// pairs having even LCM and odd LCM
static void LCMPairs(int arr[], int N)
{
   
    // Store the total number of pairs
    int total_pairs = (N * (N - 1)) / 2;
 
    // Stores the count of odd
    // numbers in the array
    int odd = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
        if ((arr[i] & 1) != 0)
            odd++;
    }
 
    // Update the count of pairs with odd LCM
    odd = (odd * (odd - 1)) / 2;
 
    // Print the count of required pairs
    System.out.println("Even = " +
                       (total_pairs - odd)  +
                       ", Odd = " + odd);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 6, 5, 4 };
    int N = arr.length;
    LCMPairs(arr, N);
}
}
  
// This code is contributed by splevel62.


Python3




# Python 3 program for the above approach
 
# Function to find count of distinct
# pairs having even LCM and odd LCM
def LCMPairs(arr, N):
    # Store the total number of pairs
    total_pairs = (N * (N - 1)) / 2
 
    # Stores the count of odd
    # numbers in the array
    odd = 0
 
    # Traverse the array arr[]
    for i in range(N):
        if (arr[i] & 1):
            odd += 1
 
    # Update the count of pairs with odd LCM
    odd = (odd * (odd - 1)) // 2
 
    # Print the count of required pairs
    print("Even =",int(total_pairs - odd),","," Odd =",odd)
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 6, 5, 4]
    N = len(arr)
    LCMPairs(arr, N)
 
     # This code is contributed by ipg2016107.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to find count of distinct
  // pairs having even LCM and odd LCM
  static void LCMPairs(int[] arr, int N)
  {
    // Store the total number of pairs
    int total_pairs = (N * (N - 1)) / 2;
 
    // Stores the count of odd
    // numbers in the array
    int odd = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
      if ((arr[i] & 1) != 0)
        odd++;
    }
 
    // Update the count of pairs with odd LCM
    odd = (odd * (odd - 1)) / 2;
 
    // Print the count of required pairs
    Console.Write("Even = " + (total_pairs - odd) + ", Odd = " + odd);
  }
 
  // Driver code
  static void Main()
  {
    int[] arr = { 3, 6, 5, 4 };
    int N = arr.Length;
    LCMPairs(arr, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript




<script>
// javascript program to implement
// the above approach
 
    // Function to find count of distinct
    // pairs having even LCM and odd LCM
    function LCMPairs(arr , N) {
 
        // Store the total number of pairs
        var total_pairs = (N * (N - 1)) / 2;
 
        // Stores the count of odd
        // numbers in the array
        var odd = 0;
 
        // Traverse the array arr
        for (i = 0; i < N; i++) {
            if ((arr[i] & 1) != 0)
                odd++;
        }
 
        // Update the count of pairs with odd LCM
        odd = (odd * (odd - 1)) / 2;
 
        // Print the count of required pairs
        document.write("Even = " + (total_pairs - odd) + ", Odd = " + odd);
    }
 
    // Driver Code
     
        var arr = [ 3, 6, 5, 4 ];
        var N = arr.length;
        LCMPairs(arr, N);
 
// This code contributed by aashish1995
</script>


Output: 

Even = 5, Odd = 1

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 11 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads