📜  数组中具有偶数和奇数 LCM 的对数

📅  最后修改于: 2021-09-07 02:32:35             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是计算具有偶数 LCM 和奇数 LCM 的对的数量。

例子:

朴素的方法:最简单的方法是生成所有可能的对以获得所有不同的对,并为每对计算它们的 LCM。如果他们的 LCM 是偶数,则增加偶数的计数。否则,增加奇数。最后,分别打印它们的计数。
时间复杂度: O((N 2 )*log(M)),其中 M 是数组中最小元素 
辅助空间: O(1)

有效的方法:为了优化上述方法,这个想法是基于这样一个事实,即2 个数字LCM 是奇数当且仅当两个数字都是奇数。因此,找到数组中的总奇数对并获得偶数 LCM 对的计数,从可能对的总数中减去奇数对的计数。

请按照以下步骤解决问题:

  • 将对的总数存储在一个变量中,比如totalPairs 。将totalPairs初始化为(N*(N – 1))/2
  • 将数组中奇数元素的数量存储在一个变量中,比如cnt
  • 将仅由奇数组成的对的计数存储在一个变量中,比如odd 。因此,奇数 = (cnt*(cnt – 1))/2
  • 完成以上步骤后,用odd LCM打印odd作为对的计数值。打印(totalPairs –odd)作为具有偶数 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.


Javascript


输出:
Even = 5, Odd = 1

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live