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.
- {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: 6
Explanation:
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.
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 = 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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumCount( int arr[], int N)
{
int odd = 0;
int even = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2)
odd++;
else
even++;
}
if (odd and even)
return N - 2;
else
return N - 1;
}
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
import java.io.*;
class GFG {
static int maximumCount( int arr[], int N)
{
int odd = 0 ;
int even = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 == 1 )
odd++;
else
even++;
}
if (odd > 0 && even > 0 )
return N - 2 ;
else
return N - 1 ;
}
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));
}
}
|
Python3
def maximumCount(arr, N):
odd = 0
even = 0
for i in range (N):
if (arr[i] % 2 ):
odd + = 1
else :
even + = 1
if (odd and even):
return N - 2
else :
return N - 1
if __name__ = = '__main__' :
arr = [ 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 ]
N = len (arr)
print (maximumCount(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximumCount( int []arr, int N)
{
int odd = 0;
int even = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 !=0)
odd++;
else
even++;
}
if (odd!=0 && even!=0)
return N - 2;
else
return N - 1;
}
public static void Main()
{
int []arr = { 9, 13, 15, 3, 16, 9, 13, 18 };
int N = arr.Length;
Console.Write(maximumCount(arr, N));
}
}
|
Javascript
<script>
function maximumCount(arr, N)
{
let odd = 0;
let even = 0;
for (let i = 0; i < N; i++) {
if (arr[i] % 2)
odd++;
else
even++;
}
if (odd && even)
return N - 2;
else
return N - 1;
}
let arr = [9, 13, 15, 3, 16, 9, 13, 18];
let N = arr.length;
document.write(maximumCount(arr, N));
</script>
|
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 May, 2022
Like Article
Save Article