📌  相关文章
📜  给定圆形数组中具有偶数的相邻对的最大计数

📅  最后修改于: 2022-05-13 01:56:05.067000             🧑  作者: Mango

给定圆形数组中具有偶数的相邻对的最大计数

给定一个包含N个整数的圆形二进制数组arr[] ,任务是找到其和为偶数的相邻元素对的最大计数,其中每个元素最多可以属于一对。

例子:

方法:给定的问题可以使用贪心方法来解决。可以观察到,所需的对只能由相同的元素组成,即(0, 0)(1, 1) 。此外,可以由X个连续的 10 组成的对数是 floor(X / 2)。因此遍历给定的数组并计算所有可能的连续整数集,并将每个集的X / 2添加到答案中。
另外,检查第一个集合和最后一组连续整数是否具有相同的值并且两个集合中的元素数是否为奇数,然后将答案加 1。

下面是上述方法的实现:

C++
//C++ program for the above approach
#include 
using namespace std;
 
// Function to find maximum count of
// pairs of adjacent elements with even
// sum in the given circular array
void findMaximumPairs(vector arr)
{
   
    // Stores the value of current
    // integer during traversal
    int currentElement = arr[0], count = 1;
 
    // First chain's count and total number
    // of pairs is initially 0
    int firstChainCount = 0, totalPairs = 0;
 
    // flag is used to check if the current
    // chain is the first chain in array
    bool flag = true;
 
    for (int i = 1; i < arr.size(); i++)
    {
       
        // Count the number of
        // consecutive elements
        if (arr[i] == currentElement) {
            count++;
        }
        else {
 
            if (flag == true) {
 
                // Stores the count of
                // elements in 1st set
                firstChainCount = count;
                flag = false;
            }
 
            // Update answer
            totalPairs = totalPairs + count / 2;
 
            // Update current integer
            currentElement = arr[i];
            count = 1;
        }
    }
 
    totalPairs = totalPairs + count / 2;
    int lastChainCount = count;
 
    if (arr[0] == arr[arr.size() - 1]
        && firstChainCount % 2 == 1
        && lastChainCount % 2 == 1)
        totalPairs++;
 
    // Print Answer
    cout << (totalPairs);
}
 
// Driver Code
int main()
{
    vector arr = { 1, 1, 1, 0, 1, 1, 0, 0 };
    findMaximumPairs(arr);
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
    // Function to find maximum count of
    // pairs of adjacent elements with even
    // sum in the given circular array
    public static void findMaximumPairs(int[] arr)
    {
        // Stores the value of current
        // integer during traversal
        int currentElement = arr[0], count = 1;
 
        // First chain's count and total number
        // of pairs is initially 0
        int firstChainCount = 0, totalPairs = 0;
 
        // flag is used to check if the current
        // chain is the first chain in array
        boolean flag = true;
 
        for (int i = 1; i < arr.length; i++) {
            // Count the number of
            // consecutive elements
            if (arr[i] == currentElement) {
                count++;
            }
            else {
 
                if (flag == true) {
 
                    // Stores the count of
                    // elements in 1st set
                    firstChainCount = count;
                    flag = false;
                }
 
                // Update answer
                totalPairs = totalPairs + count / 2;
 
                // Update current integer
                currentElement = arr[i];
                count = 1;
            }
        }
 
        totalPairs = totalPairs + count / 2;
        int lastChainCount = count;
 
        if (arr[0] == arr[arr.length - 1]
            && firstChainCount % 2 == 1
            && lastChainCount % 2 == 1)
            totalPairs++;
 
        // Print Answer
        System.out.println(totalPairs);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 1, 1, 0, 1, 1, 0, 0 };
        findMaximumPairs(arr);
    }
}


Python
# Python program for the above approach
import math
 
# Function to find maximum count of
# pairs of adjacent elements with even
# sum in the given circular array
def findMaximumPairs(arr):
   
    # Stores the value of current
    # integer during traversal
    currentElement = arr[0]
    count = 1
 
    # First chain's count and total number
    # of pairs is initially 0
    firstChainCount = 0
    totalPairs = 0
 
    # flag is used to check if the current
    # chain is the first chain in array
    flag = True;
 
    for i in range(1, len(arr)):
       
        # Count the number of
        # consecutive elements
        if (arr[i] == currentElement):
            count = count + 1
             
        else:
            if (flag == True):
 
                # Stores the count of
                # elements in 1st set
                firstChainCount = count
                flag = False
 
            # Update answer
            totalPairs = totalPairs + math.floor(count / 2)
 
            # Update current integer
            currentElement = arr[i]
            count = 1
 
    totalPairs = totalPairs + math.floor(count / 2)
    lastChainCount = count
 
    if (arr[0] == arr[len(arr) - 1]
        and firstChainCount % 2 == 1
        and lastChainCount % 2 == 1):
        totalPairs = totalPairs + 1
 
    # Print Answer
    print(totalPairs)
 
# Driver Code
arr = [ 1, 1, 1, 0, 1, 1, 0, 0 ]
findMaximumPairs(arr)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to implement above approach
using System;
class GFG {
     
    // Function to find maximum count of
    // pairs of adjacent elements with even
    // sum in the given circular array
    static void findMaximumPairs(int[] arr)
    {
        // Stores the value of current
        // integer during traversal
        int currentElement = arr[0], count = 1;
 
        // First chain's count and total number
        // of pairs is initially 0
        int firstChainCount = 0, totalPairs = 0;
 
        // flag is used to check if the current
        // chain is the first chain in array
        bool flag = true;
 
        for (int i = 1; i < arr.Length; i++) {
            // Count the number of
            // consecutive elements
            if (arr[i] == currentElement) {
                count++;
            }
            else {
 
                if (flag == true) {
 
                    // Stores the count of
                    // elements in 1st set
                    firstChainCount = count;
                    flag = false;
                }
 
                // Update answer
                totalPairs = totalPairs + count / 2;
 
                // Update current integer
                currentElement = arr[i];
                count = 1;
            }
        }
 
        totalPairs = totalPairs + count / 2;
        int lastChainCount = count;
 
        if (arr[0] == arr[arr.Length - 1]
            && firstChainCount % 2 == 1
            && lastChainCount % 2 == 1)
            totalPairs++;
 
        // Print Answer
        Console.Write(totalPairs);
    }
 
    // Driver Code
    public static void Main()
    {
        int []arr = { 1, 1, 1, 0, 1, 1, 0, 0 };
        findMaximumPairs(arr);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

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