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

12 Jul, 2021
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: 2Explanation:The given array is already arranged to give the maximum count of adjacent pairs with an even sum.
{arr[0](= 5), arr[1](= 5}, the sum of the elements is 10, which is even.{arr[1](= 5), arr[2](= 1}, the sum of the elements is 6, which is even.
{arr[0](= 5), arr[1](= 5}, the sum of the elements is 10, which is even.
{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: 6Explanation:One way to obtain the maximum count is to rearrange the array as {9, 9, 3, 13, 13, 15, 16, 18}.
{arr[0](= 9), arr[1](= 9}, the sum of the elements is 18, which is even.{arr[1](= 9), arr[2](= 3}, the sum of the elements is 12, which is even.{arr[2](= 3), arr[3](= 13}, the sum of the elements is 16, which is even.{arr[3](= 13), arr[4](= 13}, the sum of the elements is 26, which is even.{arr[4](= 13), arr[5](= 15}, the sum of the elements is 28, which is even.{arr[5](= 15), arr[6](= 16}, the sum of the elements is 31, which is not even.{arr[6](= 16), arr[7](= 18}, the sum of the elements is 34, which is even.
{arr[0](= 9), arr[1](= 9}, the sum of the elements is 18, which is even.
{arr[1](= 9), arr[2](= 3}, the sum of the elements is 12, which is even.
{arr[2](= 3), arr[3](= 13}, the sum of the elements is 16, which is even.
{arr[3](= 13), arr[4](= 13}, the sum of the elements is 26, which is even.
{arr[4](= 13), arr[5](= 15}, the sum of the elements is 28, which is even.
{arr[5](= 15), arr[6](= 16}, the sum of the elements is 31, which is not even.
{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:
It is known that:Odd + Odd = EvenEven + Even = EvenEven + Odd = OddOdd + Even = OddThe total count of adjacent pairs is N-1.Therefore, the maximum count can be obtained by putting all even numbers together and then all odd numbers or vice versa.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.
It is known that:Odd + Odd = EvenEven + Even = EvenEven + Odd = OddOdd + Even = Odd
Odd + Odd = Even
Even + Even = Even
Even + Odd = Odd
Odd + Even = Odd
The total count of adjacent pairs is N-1.
Therefore, the maximum count can be obtained by putting all even numbers together and then all odd numbers or vice versa.
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:
Find the count of odd numbers and even numbers in an array and then store them in variables say odd and even.
If odd and even both are greater than 0, then print the total count N-2 as the answer.
Otherwise, print N-1 as the answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find maximum count// pair of adjacent elements with// even sumint 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 Codeint main() {    int arr[] = { 9, 13, 15, 3, 16, 9, 13, 18 };    int N = sizeof(arr) / sizeof(arr[0]);     cout << maximumCount(arr, N);     return 0;}
/*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
# Python 3 program for the above approach # Function to find maximum count# pair of adjacent elements with# even sumdef 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 Codeif __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# program for the above approachusing System;using System.Collections.Generic; class GFG{ // Function to find maximum count// pair of adjacent elements with// even sumstatic 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 Codepublic 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.
<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>
  
6
 
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
lokeshpotta20
bgangwar59
ipg2016107
array-rearrange
Arrays
Greedy
Mathematical
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Next Greater Element
Window Sliding Technique
Count pairs with given sum
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string