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

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 odd.

Examples:

Input: arr[] = {1, 2, 3}
Output: 0

Input: arr[] = {1, 3, 5, 4, 2}
Output: 3
Eliminate 3, 5 and 2 so that in the resulting array the sum of any two adjacent values is odd.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach The sum of 2 numbers is odd if one of them is odd and the other is even. This means for every pair of consecutive numbers that have the same parity, eliminate one of them, it doesn’t matter which. So the following greedy algorithm works:

• Go through all the elements in order.
• If the current number has the same parity as the previous one eliminate it, otherwise don’t.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Returns the minimum number of eliminations ` `int` `min_elimination(``int` `n, ``int` `arr []) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``// Stores the previous element ` `    ``int` `prev_val = arr; ` ` `  `    ``// Stores the new value ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `    ``{ ` `        ``int` `curr_val = arr[i]; ` ` `  `        ``// Check if the previous and current ` `        ``// values are of same parity ` `        ``if` `(curr_val % 2 == prev_val % 2) ` `            ``count++; ` ` `  `        ``// Previous value is now the current value ` `        ``prev_val = curr_val; ` `    ``} ` ` `  `    ``// Return the counter variable ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr [] = { 1, 2, 3, 7, 9 }; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` ` `  `    ``cout << min_elimination(n, arr); ` `     `  `    ``return` `0; ` `} ` ` `  `// This code is contributed by ihritik `

## Java

 `// Java implementation of the above approach ` ` `  `class` `GFG { ` ` `  `    ``// Returns the minimum number of eliminations ` `    ``static` `int` `min_elimination(``int` `n, ``int``[] arr) ` `    ``{ ` `        ``int` `count = ``0``; ` ` `  `        ``// Stores the previous element ` `        ``int` `prev_val = arr[``0``]; ` ` `  `        ``// Stores the new value ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``int` `curr_val = arr[i]; ` ` `  `            ``// Check if the previous and current ` `            ``// values are of same parity ` `            ``if` `(curr_val % ``2` `== prev_val % ``2``) ` `                ``count++; ` ` `  `            ``// Previous value is now the current value ` `            ``prev_val = curr_val; ` `        ``} ` ` `  `        ``// Return the counter variable ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = ``new` `int``[] { ``1``, ``2``, ``3``, ``7``, ``9` `}; ` `        ``int` `n = arr.length; ` ` `  `        ``System.out.println(min_elimination(n, arr)); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the above approach ` ` `  `# Returns the minimum number of eliminations ` `def` `min_elimination(n, arr): ` ` `  `    ``count ``=` `0` ` `  `    ``# Stores the previous element ` `    ``prev_val ``=` `arr[``0``] ` ` `  `    ``# Stores the new value ` `    ``for` `i ``in` `range` `(``1``, n):  ` `        ``curr_val ``=` `arr[i]; ` ` `  `        ``# Check if the previous and current ` `        ``# values are of same parity ` `        ``if` `(curr_val ``%` `2` `=``=` `prev_val ``%` `2``): ` `            ``count ``=` `count ``+` `1` ` `  `        ``# Previous value is now the current value ` `        ``prev_val ``=` `curr_val ` `     `  ` `  `    ``# Return the counter variable ` `    ``return` `count ` ` `  `# Driver code ` `arr ``=` `[ ``1``, ``2``, ``3``, ``7``, ``9` `] ` `n ``=` `len``(arr) ` ` `  `print``(min_elimination(n, arr)); ` ` `  `# This code is contributed by ihritik `

## C#

 `// C# implementation of the above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Returns the minimum number of eliminations ` `    ``static` `int` `min_elimination(``int` `n, ``int``[] arr) ` `    ``{ ` `        ``int` `count = 0; ` ` `  `        ``// Stores the previous element ` `        ``int` `prev_val = arr; ` ` `  `        ``// Stores the new value ` `        ``for` `(``int` `i = 1; i < n; i++)  ` `        ``{ ` `            ``int` `curr_val = arr[i]; ` ` `  `            ``// Check if the previous and current ` `            ``// values are of same parity ` `            ``if` `(curr_val % 2 == prev_val % 2) ` `                ``count++; ` ` `  `            ``// Previous value is now the current value ` `            ``prev_val = curr_val; ` `        ``} ` ` `  `        ``// Return the counter variable ` `        ``return` `count; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = ``new` `int``[] { 1, 2, 3, 7, 9 }; ` `        ``int` `n = arr.Length; ` ` `  `        ``Console.WriteLine(min_elimination(n, arr)); ` `    ``} ` `} ` ` `  `// This code is contributed by ihritik `

## PHP

 ` `

Output:

```2
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : ihritik, AnkitRai01

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.