Minimum removals to make array sum even

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.

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

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 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);           cout <

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



Output:

1

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

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.

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.