

Check if a given array can be divided into pairs with even sum
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.

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:
- Find the total number of odd and even elements present in the given array and store it in two variables, countEven and countOdd respectively.
- Check if both countEven and countOdd are even or not. If found to be true, print “Yes”.
- 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;
}
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.
Recommend
-
10
MathWhy is a minute divided into 60 seconds, an hour into 60 minutes, yet there are only 24 hours in a day? Credit:
-
20
(Python) Dictionary divided into two based on a property advertisements I want to split a dictionary in two based on whether any of an array o...
-
8
Like Article Count all possible pairs in given Array with product KLast Updated : 12 Jan, 2022Given an integer arra...
-
6
2183. Count Array Pairs Divisible by K
-
3
Find all pairs with a given sumFind all pairs with a given sum | SDE Sheet | HashingHi. In this video, we talkedVideo Player is loading.
-
6
In this article, we will discuss different ways to sort an array of std::pair in C++. Table Of Contents Problem Statement In this case, we are given an array of pairs, which needs to...
-
10
Check if given Array can be formed by increasing or decreasing elementGiven an array arr[] of length N with N integers. Assume all the elements of array arr[] are e...
-
7
Counting pairs in an Array with given conditionSkip to content
-
5
Check if given Array can be formed from initial Array having one elementi_am_gauravGiven array A[], the task for this problem is to check whether A...
-
9
How to check an Array Contains an Object of Attribute with Given Value in JavaScript ?Sk...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK