给定大小为N的数组arr [] ,任务是计算具有偶数LCM和奇数LCM的线对数。
例子:
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.
天真的方法:最简单的方法是生成所有可能的对以获得所有不同的对,并为每个对计算其LCM。如果他们的LCM是偶数,则增加偶数。否则,增加奇数。最后,分别打印其计数。
时间复杂度: O((N 2 )* log(M)),其中M是数组中最小的元素
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下事实:当且仅当两个数字均为奇数时,两个数字的LCM才为奇数。因此,找到阵列中的总奇数对,并获得偶数LCM的对数,从可能的对总数中减去奇数对的数。
请按照以下步骤解决问题:
- 将对的总数存储在一个变量中,例如totalPairs 。将totalPairs初始化为(N *(N – 1))/ 2 。
- 将数组中奇数元素的数量存储在一个变量中,例如cnt 。
- 将仅由奇数组成的对的计数存储在变量中,例如odd 。因此,奇数=(cnt *(cnt – 1))/ 2 。
- 完成上述步骤后,用奇数LCM打印奇数作为对数的值。打印(totalPairs –奇数)作为具有偶数LCM的对的计数。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
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.
Even = 5, Odd = 1
时间复杂度: O(N)
辅助空间: O(1)