3

Check if a given array can be divided into pairs with even sum

 2 years ago
source link: https://www.geeksforgeeks.org/check-if-a-given-array-can-be-divided-into-pairs-with-even-sum/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Improve Article

Check if a given array can be divided into pairs with even sum

  • Difficulty Level : Expert
  • Last Updated : 28 Apr, 2021

Given an array arr[] consisting of N integers, the task is to check if it is possible to divide the entire array into pairs such that the sum of each pair is even. If it is possible, print “Yes”. Otherwise, print “No”.

Examples:

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.

Naive Approach: The simplest approach to solve the problem is to traverse the given array and for each element, find an element having the same parity which has not been picked yet and mark both the elements picked to avoid repetitions. If for any element, no suitable element is found, print “No”. Otherwise, if the entire array could be partitioned into desired pairs, print “Yes”

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The idea is to observe the fact that if the count of even and odd numbers present in the given array are both even, only then, the given array can be divided into pairs having even sum by odd numbers together and even numbers together. Follow the steps below to solve the problem:

  1. Find the total number of odd and even elements present in the given array and store it in two variables, countEven and countOdd respectively.
  2. Check if both countEven and countOdd are even or not. If found to be true, print “Yes”.
  3. Otherwise, print “No”.

Below is the implementation of the above approach:

  • Python3
  • Javascript
// C++ program for the above approach
#include <bits/stdc++.h>
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;
}
Output
Yes

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK