Skip to content
Related Articles

Related Articles

Count of adjacent pairs in given Array with even sum

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 17 Jan, 2022

Given an array arr[] of N integers, the task is to find the count of pairs of adjacent elements whose sum is even where each element can belong to at most one pair.

Example:

Input: arr[] = {1, 12, 1, 3, 5}
Output:  1
Explanation: 1 pair can be formed with arr[3] and arr[4].

Input: arr[] = {1, 2, 3, 4, 5, 6,  8, 9}
Output: 0

 

Approach: The given problem can be solved using a greedy approach. It can be observed that the required pairs can be formed of the elements having same parity only i.e, either (odd, odd) or (even, even). Also, the number of pairs that can be formed from X consecutive odd or even is floor (X / 2). Hence traverse the given array and calculate all the possible sets of consecutive integers and add (X / 2) for each set into the answer.

Below is the implementation of the above approach:

C++




// c++ program for the above approach
#include <bits/stdc++.h>
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<int>& 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<int> 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




<script>
        // JavaScript code for the above approach
 
        // Function to find maximum count of
        // pairs of adjacent elements with even
        // sum in the given array
        function findMaximumPairs(arr) {
 
            // Total number of pairs is initially 0
            let totalPairs = 0;
            for (let 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
        let arr = [1, 12, 1, 3, 5];
        document.write(
            findMaximumPairs(arr));
 
  // This code is contributed by Potta Lokesh
    </script>

 
 

Output

1

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!