📜  数组中奇数和偶数对的计数

📅  最后修改于: 2021-05-04 07:08:38             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是找到具有奇数和的对数和具有偶数和的对数。

例子:

天真的方法:
解决此问题的幼稚方法是遍历数组中的每对元素,检查它们的和,然后计算具有奇数和偶数和的对的数量。此方法将花费O(N * N)时间。

高效方法:

  • 计算数组中奇数和偶数元素的数量,并将它们存储在变量cntEvencntOdd中
  • 为了使偶数和为偶数,所有偶数元素仅与偶数元素配对,所有奇数元素仅与奇数元素配对,计数为((cntEven *(cntEven – 1))/ 2 )+((cntOdd *(cntOdd – 1))/ 2)
  • 现在,要使总和为奇数,该对中的一个元素必须为偶数,另一个元素必须为奇数,所需的计数将为cntEven * cntOdd

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
void findPairs(int arr[], int n)
{
  
    // To store the count of even and
    // odd number from the array
    int cntEven = 0, cntOdd = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If the current element is even
        if (arr[i] % 2 == 0)
            cntEven++;
  
        // If it is odd
        else
            cntOdd++;
    }
  
    // To store the count of
    // pairs with even sum
    int evenPairs = 0;
  
    // All the even elements will make
    // pairs with each other and the
    // sum of the pair will be even
    evenPairs += ((cntEven * (cntEven - 1)) / 2);
  
    // All the odd elements will make
    // pairs with each other and the
    // sum of the pair will be even
    evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
  
    // To store the count of
    // pairs with odd sum
    int oddPairs = 0;
  
    // All the even elements will make pairs
    // with all the odd element and the
    // sum of the pair will be odd
    oddPairs += (cntEven * cntOdd);
  
    cout << "Odd pairs = " << oddPairs << endl;
    cout << "Even pairs = " << evenPairs;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(int);
  
    findPairs(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
static void findPairs(int arr[], int n)
{
  
    // To store the count of even and
    // odd number from the array
    int cntEven = 0, cntOdd = 0;
  
    for (int i = 0; i < n; i++)
    {
  
        // If the current element is even
        if (arr[i] % 2 == 0)
            cntEven++;
  
        // If it is odd
        else
            cntOdd++;
    }
  
    // To store the count of
    // pairs with even sum
    int evenPairs = 0;
  
    // All the even elements will make
    // pairs with each other and the
    // sum of the pair will be even
    evenPairs += ((cntEven * (cntEven - 1)) / 2);
  
    // All the odd elements will make
    // pairs with each other and the
    // sum of the pair will be even
    evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
  
    // To store the count of
    // pairs with odd sum
    int oddPairs = 0;
  
    // All the even elements will make pairs
    // with all the odd element and the
    // sum of the pair will be odd
    oddPairs += (cntEven * cntOdd);
  
    System.out.println("Odd pairs = " + oddPairs);
    System.out.println("Even pairs = " + evenPairs);
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = arr.length;
  
    findPairs(arr, n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to find the count of pairs 
# with odd sum and the count 
# of pairs with even sum 
def findPairs(arr, n) : 
  
    # To store the count of even and 
    # odd number from the array 
    cntEven = 0; cntOdd = 0; 
  
    for i in range(n) : 
  
        # If the current element is even 
        if (arr[i] % 2 == 0) :
            cntEven += 1; 
  
        # If it is odd 
        else :
            cntOdd += 1; 
  
    # To store the count of 
    # pairs with even sum 
    evenPairs = 0; 
  
    # All the even elements will make 
    # pairs with each other and the 
    # sum of the pair will be even 
    evenPairs += ((cntEven * (cntEven - 1)) // 2); 
  
    # All the odd elements will make 
    # pairs with each other and the 
    # sum of the pair will be even 
    evenPairs += ((cntOdd * (cntOdd - 1)) // 2); 
  
    # To store the count of 
    # pairs with odd sum 
    oddPairs = 0; 
  
    # All the even elements will make pairs 
    # with all the odd element and the 
    # sum of the pair will be odd 
    oddPairs += (cntEven * cntOdd); 
  
    print("Odd pairs = ", oddPairs); 
    print("Even pairs = ", evenPairs); 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 1, 2, 3, 4, 5 ]; 
    n = len(arr); 
  
    findPairs(arr, n); 
  
# This code is contributed by kanugargng


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
static void findPairs(int []arr, int n)
{
  
    // To store the count of even and
    // odd number from the array
    int cntEven = 0, cntOdd = 0;
  
    for (int i = 0; i < n; i++)
    {
  
        // If the current element is even
        if (arr[i] % 2 == 0)
            cntEven++;
  
        // If it is odd
        else
            cntOdd++;
    }
  
    // To store the count of
    // pairs with even sum
    int evenPairs = 0;
  
    // All the even elements will make
    // pairs with each other and the
    // sum of the pair will be even
    evenPairs += ((cntEven * (cntEven - 1)) / 2);
  
    // All the odd elements will make
    // pairs with each other and the
    // sum of the pair will be even
    evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
  
    // To store the count of
    // pairs with odd sum
    int oddPairs = 0;
  
    // All the even elements will make pairs
    // with all the odd element and the
    // sum of the pair will be odd
    oddPairs += (cntEven * cntOdd);
  
    Console.WriteLine("Odd pairs = " + oddPairs);
    Console.WriteLine("Even pairs = " + evenPairs);
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 1, 2, 3, 4, 5 };
    int n = arr.Length;
  
    findPairs(arr, n);
}
}
  
// This code is contributed by Rajput-Ji


输出:
Odd pairs = 6
Even pairs = 4

复杂度分析:

  • 时间复杂度: O(N)。
    在for循环的每次迭代中,将处理来自任一数组的元素。因此,时间复杂度为O(N)。
  • 辅助空间: O(1)。
    不需要任何额外的空间,因此空间复杂度是恒定的。