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.
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.
Recommended Posts:
- Minimum removals to make array sum odd
- Minimum removals from array to make max - min <= K
- Minimum removals from array to make GCD greater
- Minimum operations to make XOR of array zero
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum insertions to make a Co-prime array
- Minimum number of changes required to make the given array an AP
- Minimum array element changes to make its elements 1 to N
- Minimum operation to make all elements equal in array
- Minimum 1s to lend power to make whole array powerful
- Minimum steps to make the product of the array equal to 1
- Minimum cost to make all array elements equal
- Minimum steps to make all the elements of the array divisible by 4
- Minimum no. of operations required to make all Array Elements Zero
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.



