Given an array of N integers. The task is to eliminate the number of elements such that in the resulting array the product of any two adjacent values is even.

**Examples:**

Input :arr[] = {1, 2, 3}Output :0Input :arr[] = {1, 3, 5, 4, 2}Output :2 Remove 1 and 3.

**Approach: **The product of 2 numbers is even if any one of them is even. This means for every pair of consecutive numbers if both are odd then eliminate one of them.

So, to make the product of the adjacent elements even, either all elements should be even or alternative even-odd elements. So the following greedy algorithm works:

- Go through all the elements in order.
- If all elements are even then “0” is returned.
- If all elements are odd then “n” is returned.
- Otherwise, count the number of consecutive odd pair.
- Return the minimum count.

Below is the Python implementation –

`# Python 3 implementation of the ` `# above approach ` ` ` `# Function to find minimum number of ` `# eliminations such that product of all ` `# adjacent elements is even ` `def` `min_elimination(n, arr): ` ` ` `countOdd ` `=` `0` ` ` `counteven ` `=` `0` ` ` `# Stores the new value ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Count odd and even numbers ` ` ` `if` `(arr[i] ` `%` `2` `): ` ` ` `countOdd ` `+` `=` `1` ` ` `else` `: ` ` ` `counteven` `+` `=` `1` ` ` `# if all are even ` ` ` `if` `counteven ` `=` `=` `n: ` ` ` `return` `0` ` ` ` ` `# if all are odd ` ` ` `if` `countOdd ` `=` `=` `n: ` ` ` `return` `n ` ` ` `else` `: ` ` ` `countpair ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` `if` `(arr[i] ` `%` `2` `=` `=` `1` `and` `arr[i` `-` `1` `] ` `%` `2` `=` `=` `1` `): ` ` ` `countpair` `+` `=` `1` ` ` `return` `countpair ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `7` `, ` `9` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `print` `(min_elimination(n, arr)) ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum elements to be removed such that sum of adjacent elements is always even
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Minimum number of elements to be removed to make XOR maximum
- Minimum number of elements that should be removed to make the array good
- Find the minimum number of elements that should be removed to make an array good
- Python | Multiply Adjacent elements
- Python | Calculate difference between adjacent elements in given list
- Python - Maximum product using K elements
- Python - Elements Product till K value
- Python | Cartesian product of string elements
- Python - Product of elements using Index list
- Python | Consecutive Custom Chunked elements Product
- Shortest subarray to be removed to make all Array elements unique
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Python program to apply itertools.product to elements of a list of lists
- Maximum sum of difference of adjacent elements
- Python - Replace index elements with elements in Other List
- Count of all subsequences having adjacent elements with different parity

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.