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.