Open In App

Maximize the count of adjacent element pairs with even sum by rearranging the Array

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, arr[] of N integers, the task is to find the maximum possible count of adjacent pairs with an even sum, rearranging the array arr[].

Examples:

Input: arr[] = {5, 5, 1}
Output: 2
Explanation:
The given array is already arranged to give the maximum count of adjacent pairs with an even sum.

  1. {arr[0](= 5), arr[1](= 5}, the sum of the elements is 10, which is even.
  2. {arr[1](= 5), arr[2](= 1}, the sum of the elements is 6, which is even.

Therefore, there are totals of 2 adjacent pairs with an even sum. And it is also the maximum possible count.

Input: arr[] = {9, 13, 15, 3, 16, 9, 13, 18}
Output: 6
Explanation:
One way to obtain the maximum count is to rearrange the array as {9, 9, 3, 13, 13, 15, 16, 18}.

  1. {arr[0](= 9), arr[1](= 9}, the sum of the elements is 18, which is even.
  2. {arr[1](= 9), arr[2](= 3}, the sum of the elements is 12, which is even.
  3. {arr[2](= 3), arr[3](= 13}, the sum of the elements is 16, which is even.
  4. {arr[3](= 13), arr[4](= 13}, the sum of the elements is 26, which is even.
  5. {arr[4](= 13), arr[5](= 15}, the sum of the elements is 28, which is even.
  6. {arr[5](= 15), arr[6](= 16}, the sum of the elements is 31, which is not even.
  7. {arr[6](= 16), arr[7](= 18}, the sum of the elements is 34, which is even.

Therefore, there are a total of 6 adjacent pairs with an even sum. And it is also the maximum possible count.

Naive Approach: The simplest approach is to try every possible arrangement of the elements and then count the number of the adjacent pairs with an even sum.

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

Efficient Approach: The above approach can be optimized based on the following observations:

  1. It is known that:
    • Odd + Odd = Even
    • Even + Even = Even
    • Even + Odd = Odd
    • Odd + Even = Odd
  2. The total count of adjacent pairs is N-1.
  3. Therefore, the maximum count can be obtained by putting all even numbers together and then all odd numbers or vice versa.
  4. Rearranging in the above-mentioned way, there will be only one pair of adjacent elements with an odd sum which will be at the junction of even numbers and odd numbers.

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find maximum count
// pair of adjacent elements with
// even sum
int maximumCount(int arr[], int N)
{
    // Stores count of odd numbers
    int odd = 0;
 
    // Stores count of even numbers
    int even = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If arr[i]%2 is 1
        if (arr[i] % 2)
            odd++;
        // Else
        else
            even++;
    }
 
    // If odd and even both
    // are greater than 0
    if (odd and even)
        return N - 2;
    // Otherwise
    else
        return N - 1;
}
 
// Driver Code
int main()
 
{
    int arr[] = { 9, 13, 15, 3, 16, 9, 13, 18 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maximumCount(arr, N);
 
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    // Function to find maximum count
    // pair of adjacent elements with
    // even sum
    static int maximumCount(int arr[], int N)
    {
        // Stores count of odd numbers
        int odd = 0;
 
        // Stores count of even numbers
        int even = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
 
            // If arr[i]%2 is 1
            if (arr[i] % 2 == 1)
                odd++;
            // Else
            else
                even++;
        }
 
        // If odd and even both
        // are greater than 0
        if (odd > 0 && even > 0)
            return N - 2;
        // Otherwise
        else
            return N - 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 9, 13, 15, 3, 16, 9, 13, 18 };
        int N = arr.length;
 
        System.out.println(maximumCount(arr, N));
    }
}
 
    // This code is contributed by Potta Lokesh


Python3




# Python 3 program for the above approach
 
# Function to find maximum count
# pair of adjacent elements with
# even sum
def maximumCount(arr, N):
   
    # Stores count of odd numbers
    odd = 0
 
    # Stores count of even numbers
    even = 0
 
    # Traverse the array arr[]
    for i in range(N):
       
        # If arr[i]%2 is 1
        if (arr[i] % 2):
            odd += 1
        # Else
        else:
            even += 1
 
    # If odd and even both
    # are greater than 0
    if (odd and even):
        return N - 2
    # Otherwise
    else:
        return N - 1
 
# Driver Code
if __name__ == '__main__':
    arr = [9, 13, 15, 3, 16, 9, 13, 18]
    N = len(arr)
 
    print(maximumCount(arr, N))
     
    # This code is contributed by bgangwar59.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find maximum count
// pair of adjacent elements with
// even sum
static int maximumCount(int []arr, int N)
{
    // Stores count of odd numbers
    int odd = 0;
 
    // Stores count of even numbers
    int even = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If arr[i]%2 is 1
        if (arr[i] % 2 !=0)
            odd++;
        // Else
        else
            even++;
    }
 
    // If odd and even both
    // are greater than 0
    if (odd!=0 && even!=0)
        return N - 2;
    // Otherwise
    else
        return N - 1;
}
 
// Driver Code
public static void Main()
 
{
    int []arr = { 9, 13, 15, 3, 16, 9, 13, 18 };
    int N = arr.Length;
 
    Console.Write(maximumCount(arr, N));
 
}
}
 
// This code is contributed by ipg2016107.


Javascript




<script>
        // JavaScript program for the above approach
 
        // Function to find maximum count
        // pair of adjacent elements with
        // even sum
        function maximumCount(arr, N)
        {
         
            // Stores count of odd numbers
            let odd = 0;
 
            // Stores count of even numbers
            let even = 0;
 
            // Traverse the array arr[]
            for (let i = 0; i < N; i++) {
 
                // If arr[i]%2 is 1
                if (arr[i] % 2)
                    odd++;
                // Else
                else
                    even++;
            }
 
            // If odd and even both
            // are greater than 0
            if (odd && even)
                return N - 2;
            // Otherwise
            else
                return N - 1;
        }
 
        // Driver Code
 
        let arr = [9, 13, 15, 3, 16, 9, 13, 18];
        let N = arr.length;
 
        document.write(maximumCount(arr, N));
 
    // This code is contributed by Potta Lokesh
 
    </script>


Output: 

6

 

Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.



Last Updated : 24 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads