📌  相关文章
📜  给定数组中相邻对的计数,总和为偶数

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

给定数组中相邻对的计数,总和为偶数

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

例子:

方法:给定的问题可以使用贪心方法来解决。可以观察到,所需的对可以仅由具有相同奇偶性的元素组成,即(奇数,奇数)或(偶数,偶数)。此外,可以由X个连续奇数或偶数组成的对数是 floor (X / 2) 。因此遍历给定的数组并计算所有可能的连续整数集,并将每个集的(X / 2)添加到答案中。

下面是上述方法的实现:

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 array
int find_maximum_pairs(vector& arr)
{
   
    // total number of pairs is initially 0
    int totalPairs = 0;
    for (int i = 0; i < arr.size() - 1; i++)
    {
       
        // If current pair is even-even or odd-odd
        if ((arr[i] + arr[i + 1]) % 2 == 0) {
            totalPairs++;
            i++;
        }
    }
 
    // return answer
    return totalPairs;
}
 
// Driver Code
int main()
{
    vector arr = { 1, 12, 1, 3, 5 };
    cout << find_maximum_pairs(arr) << endl;
}


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 array
    public static int findMaximumPairs(int[] arr)
    {
 
        // Total number of pairs is initially 0
        int totalPairs = 0;
        for (int i = 0; i < arr.length - 1;
             i++) {
 
            // If current pair is even-even
            // or odd-odd
            if ((arr[i] + arr[i + 1]) % 2 == 0) {
                totalPairs++;
                i++;
            }
        }
 
        // Return Answer
        return totalPairs;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 12, 1, 3, 5 };
        System.out.println(
            findMaximumPairs(arr));
    }
}


Python3
# Python program for the above approach
 
# function to find maximum count of pairs of adjacent
# elements with even sum in the given array
def find_maximum_pairs(arr):
     
    # total number of pairs is initially 0
    totalPairs = 0
    i = 0
    while i < (len(arr)-1):
         
        # If current pair is even-even or odd-odd
        if ((arr[i] + arr[i + 1]) % 2 == 0):
            totalPairs += 1
            i += 1
        i += 1
             
    # return answer
    return totalPairs
 
# Driver Code
arr = [1, 12, 1, 3, 5]
print(find_maximum_pairs(arr))
     
# This code is contributed by Shubham Singh


C#
// C# program for the above approach
using System;
class GFG
{
   
  // Function to find maximum count of
  // pairs of adjacent elements with even
  // sum in the given array
  public static int findMaximumPairs(int[] arr)
  {
 
    // Total number of pairs is initially 0
    int totalPairs = 0;
    for (int i = 0; i < arr.Length - 1;
         i++) {
 
      // If current pair is even-even
      // or odd-odd
      if ((arr[i] + arr[i + 1]) % 2 == 0) {
        totalPairs++;
        i++;
      }
    }
 
    // Return Answer
    return totalPairs;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 1, 12, 1, 3, 5 };
    Console.Write(findMaximumPairs(arr));
  }
}
 
// This code is contributed by gfgking.


Javascript



输出
1

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