Given an array Arr[] of N integers. We need to write a program to find minimum number of elements needed to be removed from the array, so that sum of remaining element is even.

Examples:

Input : {1, 2, 3, 4} Output : 0 Sum is already even Input : {4, 2, 3, 4} Output : 1 We need to remove 3 to make sum even.

The idea to solve this problem is to first recall the below properties of ODDs and EVENs:

- odd + odd = even
- odd + even = odd
- even + even = even
- odd * even = even
- even * even = even
- odd * odd = odd

So, we just need to make the sum of array elements even by removing some elements from the array if needed. We can notice that sum of any number of even numbers will always be even. But the sum of odd number of odd numbers is odd. That is, 3 + 3 + 3 = 9 this is odd but 3 + 3 + 3 + 3 = 12 which is even. So, we will have to just count the number of odd elements in the array. If the count of odd elements in the array is even then we do not need to remove any element from the array but if the count of odd elements in the array is odd then by removing any one of the odd elements from the array, the sum of the array will become even.

Below is the implementation of above idea:

## C++

`// CPP program to find minimum number of ` `// elements to be removed to make the sum` `// even` ` ` `#include <iostream>` `using` `namespace` `std;` ` ` `int` `findCount(` `int` `arr[], ` `int` `n)` `{` ` ` `int` `count = 0;` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `if` `(arr[i] % 2 == 1)` ` ` `count++; ` `/* counts only odd numbers */` ` ` ` ` `/* if the counter is even return 0 ` ` ` `otherwise return 1 */` ` ` `if` `(count % 2 == 0)` ` ` `return` `0;` ` ` `else` ` ` `return` `1; ` `}` ` ` `// Driver Code ` `int` `main()` `{` ` ` `int` `arr[] = {1, 2, 4, 5, 1};` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` ` ` `cout <<findCount(arr,n);` ` ` ` ` `return` `0;` `}` |

## Java

`// Java program to find minimum number of ` `// elements to be removed to make the sum` `// even` `class` `GFG {` ` ` ` ` `static` `int` `findCount(` `int` `arr[], ` `int` `n)` ` ` `{` ` ` `int` `count = ` `0` `;` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `if` `(arr[i] % ` `2` `== ` `1` `)` ` ` ` ` `/* counts only odd numbers */` ` ` `count++;` ` ` ` ` `/* if the counter is even return 0 ` ` ` `otherwise return 1 */` ` ` `if` `(count % ` `2` `== ` `0` `)` ` ` `return` `0` `;` ` ` `else` ` ` `return` `1` `; ` ` ` `}` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `arr[] = {` `1` `, ` `2` `, ` `4` `, ` `5` `, ` `1` `};` ` ` `int` `n = arr.length;` ` ` ` ` `System.out.println(findCount(arr, n));` ` ` `}` `}` ` ` `// This code is contribute by Smitha Dinesh Semwal` |

## Python 3

`# program to find minimum number` `# of elements to be removed to ` `# make the sum even` ` ` `def` `findCount(arr, n):` ` ` ` ` `count ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n):` ` ` `if` `(arr[i] ` `%` `2` `=` `=` `1` `):` ` ` ` ` `# counts only odd` ` ` `# numbers ` ` ` `count ` `+` `=` `-` `1` ` ` ` ` `# if the counter is ` ` ` `# even return 0 ` ` ` `# otherwise return 1 ` ` ` `if` `(count ` `%` `2` `=` `=` `0` `):` ` ` `return` `0` ` ` `else` `:` ` ` `return` `1` ` ` `# Driver Code ` `arr ` `=` `[` `1` `, ` `2` `, ` `4` `, ` `5` `, ` `1` `]` `n ` `=` `len` `(arr)` ` ` `print` `(findCount(arr, n))` ` ` `# This code is contributed by` `# Smitha Dinesh Semwal` |

## C#

`// C# program to find minimum number of ` `// elements to be removed to make the sum` `// even` `using` `System;` ` ` `public` `class` `GFG{` ` ` ` ` `static` `int` `findCount(` `int` `[] arr, ` `int` `n)` ` ` `{` ` ` `int` `count = 0;` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `if` `(arr[i] % 2 == 1)` ` ` ` ` `/* counts only odd numbers */` ` ` `count++;` ` ` ` ` `/* if the counter is even return 0 ` ` ` `otherwise return 1 */` ` ` `if` `(count % 2 == 0)` ` ` `return` `0;` ` ` `else` ` ` `return` `1; ` ` ` `}` ` ` ` ` `// Driver code` ` ` `static` `public` `void` `Main ()` ` ` `{` ` ` `int` `[] arr = {1, 2, 4, 5, 1};` ` ` `int` `n = arr.Length;` ` ` ` ` `Console.WriteLine(findCount(arr, n));` ` ` `}` `}` ` ` `// This code is contributed by Ajit.` |

## PHP

`<?php` `// PHP program to find minimum number of ` `// elements to be removed to make the sum` `// even` ` ` `function` `findCount(` `$arr` `,` `$n` `)` `{` ` ` `$count` `= 0;` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `if` `(` `$arr` `[` `$i` `] % 2 == 1)` ` ` ` ` `// counts only odd numbers` ` ` `$count` `++; ` ` ` ` ` `/* if the counter is even return ` ` ` `0 otherwise return 1 */` ` ` `if` `(` `$count` `% 2 == 0)` ` ` `return` `0;` ` ` `else` ` ` `return` `1; ` `}` ` ` `// Driver Code` `$arr` `= ` `array` `(1, 2, 4, 5, 1);` `$n` `= 5;` `echo` `findCount(` `$arr` `,` `$n` `);` ` ` `// This code is contributed by ` `// Manish Shaw (manishshaw1)` `?>` |

Output:

1

**Time Complexity**: O(n), where n is the number of elements in the array.

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.