📌  相关文章
📜  检查给定的数组是否可以分成偶数对

📅  最后修改于: 2021-09-04 08:13:29             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是检查是否可以将整个数组分成对,使得每对偶数。如果可能,请打印“是” 。否则,打印“否”

例子:

朴素的方法:解决问题的最简单方法是遍历给定的数组,并为每个元素找到一个具有相同奇偶校验但尚未被选中的元素,并标记两个被选中的元素以避免重复。如果对于任何元素,没有找到合适的元素,打印“No” 。否则,如果可以将整个数组划分为所需的对,则打印“Yes”

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

有效的方法:这个想法是观察这样一个事实,如果给定数组中出现的偶数和奇数的计数都是偶数,只有这样,给定的数组才能被分成奇数和偶数在一起的偶数对和偶数对.请按照以下步骤解决问题:

  1. 找出给定数组中存在的奇数和偶数元素的总数,并将其存储在两个变量中,分别是countEvencountOdd
  2. 检查countEvencountOdd是否为偶数。如果发现是真的,打印“是”
  3. 否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if we can split
// array into pairs of even sum or not
bool canPairs(int arr[], int n)
{
    // If the length is odd then it
    // is not possible to make pairs
    if (n % 2 == 1)
        return false;
 
    // Initialize count of odd & even
    int odd_count = 0, even_count = 0;
 
    // Iterate through the array
    for (int i = 0; i < n; i++)
    {
        // Count even element
        if (arr[i] % 2 == 0)
            even_count++;
        else
            odd_count++;
    }
 
    // If count of even elements
    // and odd elements are even
    if (even_count % 2 == 0 && odd_count % 2 == 0)
    {
        return true;
    }
 
    return false;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 1, 4, 7, 5 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    if (canPairs(arr, N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to check if we can split
    // array into pairs of even sum or not
    static boolean canPairs(int[] arr, int n)
    {
        // If the length is odd then it
        // is not possible to make pairs
        if (n % 2 == 1)
            return false;
 
        // Initialize count of odd & even
        int odd_count = 0, even_count = 0;
 
        // Iterate through the array
        for (int i = 0; i < n; i++)
        {
            // Count even element
            if (arr[i] % 2 == 0)
                even_count++;
            else
                odd_count++;
        }
 
        // If count of even elements
        // and odd elements are even
        if (even_count % 2 == 0 && odd_count % 2 == 0)
        {
            return true;
        }
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 3, 2, 1, 4, 7, 5 };
        int N = arr.length;
 
        // Function call
        if (canPairs(arr, N))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to check if we can split
# array into pairs of even sum or not
 
 
def canPairs(arr, n):
 
    # If the length is odd then it
    # is not possible to make pairs
    if (n % 2 == 1):
        return False
 
    # Initialize count of odd & even
    odd_count = 0
    even_count = 0
 
    # Iterate through the array
    for i in range(0, n):
 
        # Count even element
        if (arr[i] % 2 == 0):
            even_count = even_count + 1
        else:
            odd_count = odd_count + 1
 
    # If count of even elements
    # and odd elements are even
    if ((even_count % 2 == 0) and
            (odd_count % 2 == 0)):
        return True
 
    return False
 
 
# Driver Code
if __name__ == '__main__':
 
    arr = [3, 2, 1, 4, 7, 5]
    N = len(arr)
 
    # Function call
    if (canPairs(arr, N)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to check if we can split
    // array into pairs of even sum or not
    static bool canPairs(int[] arr, int n)
    {
 
        // If the length is odd then it
        // is not possible to make pairs
        if (n % 2 == 1)
            return false;
 
        // Initialize count of odd & even
        int odd_count = 0, even_count = 0;
 
        // Iterate through the array
        for (int i = 0; i < n; i++)
        {
            // Count even element
            if (arr[i] % 2 == 0)
                even_count++;
            else
                odd_count++;
        }
 
        // If count of even elements
        // and odd elements are even
        if (even_count % 2 == 0 && odd_count % 2 == 0)
        {
            return true;
        }
        return false;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 3, 2, 1, 4, 7, 5 };
        int N = arr.Length;
 
        // Function call
        if (canPairs(arr, N))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by akhilsaini


Javascript


输出
Yes

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

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