Count of pairs having even and odd LCM from an array
Last Updated :
11 Nov, 2021
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)
Similar Reads
Count of odd and even sum pairs in an array Given an array arr[] of N positive integers, the task is to find the number of pairs with odd sum and the number of pairs with even sum.Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: Odd pairs = 6 Even pairs = 4Input: arr[] = {7, 4, 3, 2} Output: Odd pairs = 4 Even pairs = 2 Naive Approach: The na
8 min read
Count of even and odd power pairs in an Array Given an array arr[] of length N, the task is to count the number of pairs (X, Y) such that XY is even and count the number of pairs such that XY is odd.Examples: Input: arr[] = {2, 3, 4, 5} Output: 6 6 Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are the pairs with even values and
5 min read
Find the count of even odd pairs in a given Array Given an array arr[], the task is to find the count even-odd pairs in the array.Examples: Input: arr[] = { 1, 2, 1, 3 } Output: 2 Explanation: The 2 pairs of the form (even, odd) are {2, 1} and {2, 3}. Input: arr[] = { 5, 4, 1, 2, 3} Output: 3 Naive Approach: Run two nested loops to get all the poss
7 min read
Count number of even and odd length elements in an Array Given an array arr[] of integers of size N, the task is to find the number elements of the array having even and odd length.Examples: Input: arr[] = {14, 735, 3333, 223222} Output: Number of even length elements = 3 Number of odd length elements = 1 Input: arr[] = {1121, 322, 32, 14783, 44} Output:
5 min read
Count Pairs from two arrays with even sum Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number. Note that an element will only be a part of a single pair.Examples:
7 min read