给定一个由N 个整数组成的数组arr[] ,任务是找到具有偶数和的相邻对的最大可能计数,重新排列数组arr[]。
例子:
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.
朴素的方法:最简单的方法是尝试所有可能的元素排列,然后用偶数和计算相邻对的数量。
时间复杂度: O(N*N!)
辅助空间: O(1)
高效的方法:上述方法可以基于以下观察进行优化:
- 众所周知:
- 奇数 + 奇数 = 偶数
- 偶数 + 偶数 = 偶数
- 偶数 + 奇数 = 奇数
- 奇数 + 偶数 = 奇数
- 相邻对的总数为N-1。
- 因此,可以通过将所有偶数放在一起然后将所有奇数放在一起来获得最大计数,反之亦然。
- 以上述方式重新排列,将只有一对相邻元素的奇数和将在偶数和奇数的交界处。
请按照以下步骤解决问题:
- 查找数组中奇数和偶数的计数,然后将它们存储在变量中,比如odd和even 。
- 如果奇数和偶数都大于0,则打印总数N-2作为答案。
- 否则,打印N-1作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
6
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。