给定一个由N个整数组成的数组arr [] ,任务是检查是否有可能将整个数组分成几对,以使每对的和为偶数。如果可能,请打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {3, 2, 1, 4, 7, 5, }
Output: Yes
Explanation:
The given array can be divided into pairs: {1, 3}, {2, 4}, {5, 7}.
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: No
Explanation:
No possible pair distribution exists such that each pair sum is even.
天真的方法:解决问题的最简单方法是遍历给定的数组,并为每个元素找到一个具有相同奇偶校验且尚未被选取的元素,并标记两个被选取的元素以避免重复。如果对于任何元素都找不到合适的元素,请打印“ No” 。否则,如果可以将整个阵列划分为所需的对,则打印“是” 。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效方法:想法是观察以下事实:如果给定数组中存在的偶数和奇数均是偶数,则只有这样,才能将给定数组分为几对,它们的奇数和为奇数加在一起,而偶数加在一起。请按照以下步骤解决问题:
- 查找给定数组中存在的奇数和偶数元素的总数,并将其存储在两个变量中,分别为countEven和countOdd 。
- 检查countEven和countOdd是否都为偶数。如果发现是真的,则打印“是” 。
- 否则,打印“否” 。
下面是上述方法的实现:
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
输出
Yes
时间复杂度: O(N)
辅助空间: O(1)