Minimum removals to make array sum odd
Given an array Arr[] of N integers. The task is to find minimum number of elements needed to be removed from the array, so that sum of remaining elements is odd. Considering there is atleast one odd number.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 1
Remove 1 to make array sum odd.
Input: arr[] = {4, 2, 3, 4}
Output: 0
Sum is already odd.
Approach: 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
As the sum of any number of even numbers is even. So the count of even doesn’t matter.
And the sum of odd number of odd number is always odd. So to make array sum odd, count of odd numbers must be odd.
So, count the number of odd numbers and check if the count is odd then no removals required. Else the count is even then 1 odd number needs to be removed.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to find minimum removals int findCount(int arr[], int n) { // Count odd numbers int countOdd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 == 1) countOdd++; // If the counter is odd return 0 // otherwise return 1 if (countOdd % 2 == 0) return 1; else return 0; } // Driver Code int main() { int arr[] = { 1, 2, 3, 5, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findCount(arr, n); return 0; } |
Java
// Java implementation of the above approach class GfG { // Function to find minimum removals static int findCount(int arr[], int n) { // Count odd numbers int countOdd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 == 1) countOdd++; // If the counter is odd return 0 // otherwise return 1 if (countOdd % 2 == 0) return 1; else return 0; } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 3, 5, 1 }; int n = arr.length; System.out.println(findCount(arr, n)); } } // This code is contributed by // Prerna Saini |
Python3
# Python3 implementation of the # above approach # Function to find minimum removals def findCount(arr, n) : # Count odd numbers countOdd = 0; for i in range(n) : if (arr[i] % 2 == 1) : countOdd += 1; # If the counter is odd return 0 # otherwise return 1 if (countOdd % 2 == 0) : return 1; else : return 0; # Driver Code if __name__ == "__main__" : arr = [ 1, 2, 3, 5, 1 ]; n = len(arr) ; print(findCount(arr, n)); # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GfG { // Function to find minimum removals static int findCount(int []arr, int n) { // Count odd numbers int countOdd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 == 1) countOdd++; // If the counter is odd return 0 // otherwise return 1 if (countOdd % 2 == 0) return 1; else return 0; } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 3, 5, 1 }; int n = arr.Length; Console.WriteLine(findCount(arr, n)); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the above approach // Function to find minimum removals function findCount($arr, $n) { // Count odd numbers $countOdd = 0; for ($i = 0; $i < $n; $i++) if ($arr[$i] % 2 == 1) $countOdd++; // If the counter is odd return 0 // otherwise return 1 if ($countOdd % 2 == 0) return 1; else return 0; } // Driver Code $arr = array(1, 2, 3, 5, 1); $n = sizeof($arr); echo(findCount($arr, $n)); // This code is contributed by // Code_Mech. ?> |
1
Recommended Posts:
- Minimum removals from array to make max - min <= K
- Minimum removals to make array sum even
- Minimum removals from array to make GCD greater
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum operations to make XOR of array zero
- Minimum array element changes to make its elements 1 to N
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum number of changes required to make the given array an AP
- Minimum insertions to make a Co-prime array
- Minimum 1s to lend power to make whole array powerful
- Minimum replacements to make elements of a ternary array same
- Minimum cost to make all array elements equal
- Minimum no. of operations required to make all Array Elements Zero
- Minimum number of operations on an array to make all elements 0
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.


