Check if a given array can be divided into pairs with even sum
Last Updated :
13 Sep, 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:
C++
#include <bits/stdc++.h>
using namespace std;
bool canPairs( int arr[], int n)
{
if (n % 2 == 1)
return false ;
int odd_count = 0, even_count = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even_count++;
else
odd_count++;
}
if (even_count % 2 == 0 && odd_count % 2 == 0)
{
return true ;
}
return false ;
}
int main()
{
int arr[] = { 3, 2, 1, 4, 7, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
if (canPairs(arr, N)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean canPairs( int [] arr, int n)
{
if (n % 2 == 1 )
return false ;
int odd_count = 0 , even_count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 == 0 )
even_count++;
else
odd_count++;
}
if (even_count % 2 == 0 && odd_count % 2 == 0 )
{
return true ;
}
return false ;
}
public static void main(String[] args)
{
int [] arr = { 3 , 2 , 1 , 4 , 7 , 5 };
int N = arr.length;
if (canPairs(arr, N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def canPairs(arr, n):
if (n % 2 = = 1 ):
return False
odd_count = 0
even_count = 0
for i in range ( 0 , n):
if (arr[i] % 2 = = 0 ):
even_count = even_count + 1
else :
odd_count = odd_count + 1
if ((even_count % 2 = = 0 ) and
(odd_count % 2 = = 0 )):
return True
return False
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 , 4 , 7 , 5 ]
N = len (arr)
if (canPairs(arr, N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool canPairs( int [] arr, int n)
{
if (n % 2 == 1)
return false ;
int odd_count = 0, even_count = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even_count++;
else
odd_count++;
}
if (even_count % 2 == 0 && odd_count % 2 == 0)
{
return true ;
}
return false ;
}
public static void Main()
{
int [] arr = { 3, 2, 1, 4, 7, 5 };
int N = arr.Length;
if (canPairs(arr, N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function canPairs(arr, n)
{
if (n % 2 == 1)
return false ;
let odd_count = 0, even_count = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even_count++;
else
odd_count++;
}
if (even_count % 2 == 0 &&
odd_count % 2 == 0)
{
return true ;
}
return false ;
}
let arr = [ 3, 2, 1, 4, 7, 5 ];
let N = arr.length;
if (canPairs(arr, N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...