📜  阵列中偶数和奇数功率对的计数

📅  最后修改于: 2021-04-22 00:14:34             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是计算对(X,Y)的对数,以使X Y为偶数,并计算对的数以使X Y为奇数。

例子:

天真的方法:计算每个可能的对的功效,并找到计算出的值是偶数还是奇数。

高效的方法:计算数组中的偶数和奇数元素,然后使用pow(偶数,除自身以外的任何元素)为偶数,pow(奇数,除自身以外的任何元素)为奇数的概念。

因此,对数(X,Y)为,

  • pow(X,Y)是偶数=(偶数的数量*(n – 1))
  • pow(X,Y)是奇数=(奇数个数*(n – 1))

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find and print the
// required count of pairs
void countPairs(int arr[], int n)
{
  
    // Find the count of even and
    // odd elements in the array
    int even = 0, odd = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            even++;
        else
            odd++;
    }
  
    // Print the required count of pairs
    cout << (even) * (n - 1) << endl;
    cout << (odd) * (n - 1) << endl;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    countPairs(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
    // Function to find and print the
    // required count of pairs
    static void countPairs(int arr[], int n)
    {
  
        // Find the count of even and
        // odd elements in the array
        int even = 0, odd = 0;
        for (int i = 0; i < n; i++)
        {
            if (arr[i] % 2 == 0)
                even++;
            else
                odd++;
        }
  
        // Print the required count of pairs
        System.out.println((even) * (n - 1));
        System.out.println((odd) * (n - 1));
    }
  
    // Driver code
    public static void main(String args[]) 
    {
        int arr[] = { 2, 3, 4, 5 };
        int n = arr.length; 
  
        countPairs(arr, n);
    }
}
  
// This code is contributd by ANKITUMAR34


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Function to find and print the 
    // required count of pairs 
    static void countPairs(int []arr, int n) 
    { 
  
        // Find the count of even and 
        // odd elements in the array 
        int even = 0, odd = 0; 
        for (int i = 0; i < n; i++) 
        { 
            if (arr[i] % 2 == 0) 
                even++; 
            else
                odd++; 
        } 
  
        // Print the required count of pairs 
        Console.WriteLine((even) * (n - 1)); 
        Console.WriteLine((odd) * (n - 1)); 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int []arr = { 2, 3, 4, 5 }; 
        int n = arr.Length; 
  
        countPairs(arr, n); 
    } 
} 
  
// This code is contributd by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to find and print the 
# required count of pairs
def countPairs(arr, n): 
      
    # Find the count of even and 
    # odd elements in the array
    odd = 0
    even = 0
    for i in range(n): 
        if (arr[i] % 2 == 0): 
            even += 1
        else: 
            odd += 1
              
    # Count the number of odd pairs 
    odd_pairs = odd*(n-1)
  
    # Count the number of even pairs
    even_pairs = even*(n-1)
  
    print(odd_pairs)
    print(even_pairs)
  
# Driver code 
if __name__ == '__main__': 
    arr = [2, 3, 4, 5] 
    n = len(arr) 
    countPairs(arr, n)


输出:
6
6