📌  相关文章
📜  通过重新排列数组来最大化具有偶数和的相邻元素对的计数

📅  最后修改于: 2021-10-26 05:39:14             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到具有偶数和的相邻对的最大可能计数,重新排列数组arr[]。

例子:

朴素的方法:最简单的方法是尝试所有可能的元素排列,然后用偶数和计算相邻对的数量。

时间复杂度: O(N*N!)
辅助空间: O(1)

高效的方法:上述方法可以基于以下观察进行优化:

  1. 众所周知:
    • 奇数 + 奇数 = 偶数
    • 偶数 + 偶数 = 偶数
    • 偶数 + 奇数 = 奇数
    • 奇数 + 偶数 = 奇数
  2. 相邻对的总数为N-1。
  3. 因此,可以通过将所有偶数放在一起然后将所有奇数放在一起来获得最大计数,反之亦然。
  4. 以上述方式重新排列,将只有一对相邻元素的奇数和将在偶数和奇数的交界处。

请按照以下步骤解决问题:

  • 查找数组中奇数和偶数的计数,然后将它们存储在变量中,比如oddeven
  • 如果奇数偶数都大于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 现场工作专业课程学生竞争性编程现场课程