📜  用偶数和奇数和计数对有序对的数目

📅  最后修改于: 2021-04-28 13:54:33             🧑  作者: Mango

给定一个由n个正数组成的数组,任务是计算带有偶数和奇数和的有序对的数量。

例子:

方法:

如果一个数字为奇数而另一个数字为偶数,则两个数字的总和为奇数。因此,现在我们必须计算偶数和奇数。正如(a,b)和(b,a)对一样,它们都被视为不同的对,因此

这是因为每个偶数都可以与每个奇数配对,并且每个奇数都可以与每个偶数配对。因此,答案是乘以2。

并且偶数和对的数量将是奇数和对的数量的倒数。所以:

下面是上述方法的实现:

CPP
// C++ implementation of the above approach
#include 
using namespace std;
  
// function to count odd sum pair
int count_odd_pair(int n, int a[])
{
    int odd = 0, even = 0;
  
    for (int i = 0; i < n; i++) {
  
        // if number is even
        if (a[i] % 2 == 0)
            even++;
  
        // if number is odd
        else
            odd++;
    }
  
    // count of ordered pairs
    int ans = odd * even * 2;
  
    return ans;
}
  
// function to count even sum pair
int count_even_pair(int odd_sum_pairs, int n)
{
    int total_pairs = (n * (n - 1));
    int ans = total_pairs - odd_sum_pairs;
      
    return ans;
}
  
// Driver code
int main()
{
  
    int n = 6;
    int a[] = { 2, 4, 5, 9, 1, 8 };
  
    int odd_sum_pairs = count_odd_pair(n, a);
  
    int even_sum_pairs = count_even_pair(
        odd_sum_pairs, n);
  
    cout << "Even Sum Pairs = "
         << even_sum_pairs
         << endl;
    cout << "Odd Sum Pairs= "
         << odd_sum_pairs
         << endl;
  
    return 0;
}


Java
// Java implementation of the above approach
  
class GFG
{
    // function to count odd sum pair
    static int count_odd_pair(int n, int a[])
    {
        int odd = 0, even = 0;
      
        for (int i = 0; i < n; i++) {
      
            // if number is even
            if (a[i] % 2 == 0)
                even++;
      
            // if number is odd
            else
                odd++;
        }
      
        // count of ordered pairs
        int ans = odd * even * 2;
      
        return ans;
    }
      
    // function to count even sum pair
    static int count_even_pair(int odd_sum_pairs, int n)
    {
        int total_pairs = (n * (n - 1));
        int ans = total_pairs - odd_sum_pairs;
          
        return ans;
    }
      
    // Driver code
    public static void main(String []args)
    {
      
        int n = 6;
        int []a = { 2, 4, 5, 9, 1, 8 };
      
        int odd_sum_pairs = count_odd_pair(n, a);
      
        int even_sum_pairs = count_even_pair( odd_sum_pairs, n);
      
        System.out.println("Even Sum Pairs = " + even_sum_pairs);
              
        System.out.println("Odd Sum Pairs= " + odd_sum_pairs);
      
          
    }
  
}
  
// This code is contributed by ihritik


Python3
# Pytho3 implementation of the above approach
  
# function to count odd sum pair
def count_odd_pair( n,  a):
  
    odd = 0 
    even = 0 
  
    for i in range(0,n): 
  
        # if number is even
        if ( a[ i] % 2 == 0):
             even=even+1
  
        # if number is odd
        else:
             odd=odd+1
      
  
    # count of ordered pairs
    ans =  odd *  even * 2 
  
    return  ans 
  
  
# function to count even sum pair
def count_even_pair( odd_sum_pairs,  n):
  
    total_pairs = ( n * ( n - 1)) 
    ans =  total_pairs -  odd_sum_pairs 
    return ans 
  
  
# Driver code
  
n = 6 
a = [2, 4, 5, 9, 1, 8] 
  
odd_sum_pairs = count_odd_pair( n,  a) 
  
even_sum_pairs = count_even_pair( odd_sum_pairs,  n) 
  
print("Even Sum Pairs =", even_sum_pairs)
print("Odd Sum Pairs=", odd_sum_pairs)
      
# This code is contributed by ihritik


C#
// C# implementation of the above approach
  
using System;
class GFG
{
    // function to count odd sum pair
    static int count_odd_pair(int n, int []a)
    {
        int odd = 0, even = 0;
      
        for (int i = 0; i < n; i++) {
      
            // if number is even
            if (a[i] % 2 == 0)
                even++;
      
            // if number is odd
            else
                odd++;
        }
      
        // count of ordered pairs
        int ans = odd * even * 2;
      
        return ans;
    }
      
    // function to count even sum pair
    static int count_even_pair(int odd_sum_pairs, int n)
    {
        int total_pairs = (n * (n - 1));
        int ans = total_pairs - odd_sum_pairs;
          
        return ans;
    }
      
    // Driver code
    public static void Main()
    {
      
        int n = 6;
        int []a = { 2, 4, 5, 9, 1, 8 };
      
        int odd_sum_pairs = count_odd_pair(n, a);
      
        int even_sum_pairs = count_even_pair( odd_sum_pairs, n);
      
        Console.WriteLine("Even Sum Pairs = " + even_sum_pairs);
              
        Console.WriteLine("Odd Sum Pairs= " + odd_sum_pairs);
         
    }
  
}
  
// This code is contributed by ihritik


PHP


输出:
Even Sum Pairs = 12
Odd Sum Pairs= 18