Minimum elements to be removed such that sum of adjacent elements is always even

15 Apr, 2021
Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is even.
Examples: 
Input : arr[] = {1, 2, 3}
Output : 1
Remove 2 from the array.

Input : arr[] = {1, 3, 5, 4, 2}
Output : 2
Remove 4 and 2.
Approach: The sum of 2 numbers is even if either both of them is odd or both of them is even. This means for every pair of consecutive numbers that have the different parity, eliminate one of them.So, to make the adjacent elements sum even, either all elements should be odd or even. So the following greedy algorithm works:
Go through all the elements in order.
Count the odd and even elements in the array.
Return the minimum count.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Function to find minimum number of eliminations// such that sum of all adjacent elements is evenint min_elimination(int n, int arr[]){    int countOdd = 0;     // Stores the new value    for (int i = 0; i < n; i++)         // Count odd numbers        if (arr[i] % 2)            countOdd++;     // Return the minimum of even and    // odd count    return min(countOdd, n - countOdd);} // Driver codeint main(){    int arr[] = { 1, 2, 3, 7, 9 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << min_elimination(n, arr);     return 0;}
// Java implementation of the above approachclass GFG{     // Function to find minimum number of// eliminations such that sum of all// adjacent elements is evenstatic int min_elimination(int n, int arr[]){    int countOdd = 0;     // Stores the new value    for (int i = 0; i < n; i++)         // Count odd numbers        if (arr[i] % 2 == 1)            countOdd++;     // Return the minimum of even    // and odd count    return Math.min(countOdd, n - countOdd);} // Driver codepublic static void main(String[] args){    int arr[] = { 1, 2, 3, 7, 9 };    int n = arr.length;     System.out.println(min_elimination(n, arr));}} // This code is contributed by Code_Mech
# Python 3 implementation of the# above approach # Function to find minimum number of# eliminations such that sum of all# adjacent elements is evendef min_elimination(n, arr):    countOdd = 0     # Stores the new value    for i in range(n):                 # Count odd numbers        if (arr[i] % 2):            countOdd += 1     # Return the minimum of even and    # odd count    return min(countOdd, n - countOdd) # Driver codeif __name__ == '__main__':    arr = [1, 2, 3, 7, 9]    n = len(arr)     print(min_elimination(n, arr)) # This code is contributed by# Surendra_Gangwar
// C# implementation of the above approachusing System; class GFG{     // Function to find minimum number of// eliminations such that sum of all// adjacent elements is evenstatic int min_elimination(int n, int[] arr){    int countOdd = 0;     // Stores the new value    for (int i = 0; i < n; i++)         // Count odd numbers        if (arr[i] % 2 == 1)            countOdd++;     // Return the minimum of even    // and odd count    return Math.Min(countOdd, n - countOdd);} // Driver codepublic static void Main(){    int[] arr = { 1, 2, 3, 7, 9 };    int n = arr.Length;     Console.WriteLine(min_elimination(n, arr));}} // This code is contributed by Code_Mech
<?php// PHP implementation of the above approach // Function to find minimum number of// eliminations such that sum of all// adjacent elements is evenfunction min_elimination($n, $arr){    $countOdd = 0;     // Stores the new value    for ($i = 0; $i < $n; $i++)         // Count odd numbers        if ($arr[$i] % 2 == 1)            $countOdd++;     // Return the minimum of even    // and odd count    return min($countOdd, $n - $countOdd);} // Driver code$arr = array(1, 2, 3, 7, 9);$n = sizeof($arr); echo(min_elimination($n, $arr)); // This code is contributed by Code_Mech?>
<script>  // Function to find minimum number of eliminations// such that sum of all adjacent elements is evenfunction min_elimination(n, arr){    let countOdd = 0;     // Stores the new value    for (let i = 0; i < n; i++)         // Count odd numbers        if (arr[i] % 2)            countOdd++;     // Return the minimum of even and    // odd count    return Math.min(countOdd, n - countOdd);} // Driver code let arr= [1, 2, 3, 7, 9];let n = arr.length; document.write(min_elimination(n, arr)); </script>
1
 
Time Complexity: O(N )
Auxiliary Space: O(1)
SURENDRA_GANGWAR
Code_Mech
ujjwalgoel1103
mohit kumar 29
array-traversal-question
Marketing
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Introduction to Arrays
K'th Smallest/Largest Element in Unsorted Array | Set 1
Subset Sum Problem | DP-25
Introduction to Data Structures