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

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++

`// 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 even ` `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) ` ` ` `countOdd++; ` ` ` ` ` `// Return the minimum of even and ` ` ` `// odd count ` ` ` `return` `min(countOdd, n - countOdd); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 7, 9 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << min_elimination(n, arr); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` `// Function to find minimum number of ` `// eliminations such that sum of all ` `// adjacent elements is even ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the ` `# above approach ` ` ` `# Function to find minimum number of ` `# eliminations such that sum of all ` `# adjacent elements is even ` `def` `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 code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `7` `, ` `9` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `print` `(min_elimination(n, arr)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find minimum number of ` `// eliminations such that sum of all ` `// adjacent elements is even ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

1

## Recommended Posts:

- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Find the minimum number of elements that should be removed to make an array good
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Minimum absolute difference of adjacent elements in a circular array
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sum such that no two elements are adjacent
- Sort 1 to N by swapping adjacent elements
- Distinct adjacent elements in an array
- Maximum set bit sum in array without considering adjacent elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.