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

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output

Yes

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : akhilsaini, ArifShaikh