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++ 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;
} |
// 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 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# 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 |
<script> // Javascript program for the above approach // Function to check if we can split // array into pairs of even sum or not function 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
let odd_count = 0, even_count = 0;
// Iterate through the array
for (let 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 let arr = [ 3, 2, 1, 4, 7, 5 ]; let N = arr.length; // Function call if (canPairs(arr, N))
document.write( "Yes" );
else document.write( "No" );
// This code is contributed by target_2 </script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)