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++
#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++;
if (count % 2 == 0)
return 0;
else
return 1;
}
int main()
{
int arr[] = {1, 2, 4, 5, 1};
int n = sizeof (arr)/ sizeof (arr[0]);
cout <<findCount(arr,n);
return 0;
}
|
Java
class GFG {
static int findCount( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
if (arr[i] % 2 == 1 )
count++;
if (count % 2 == 0 )
return 0 ;
else
return 1 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 5 , 1 };
int n = arr.length;
System.out.println(findCount(arr, n));
}
}
|
Python 3
def findCount(arr, n):
count = 0
for i in range ( 0 , n):
if (arr[i] % 2 = = 1 ):
count + = - 1
if (count % 2 = = 0 ):
return 0
else :
return 1
arr = [ 1 , 2 , 4 , 5 , 1 ]
n = len (arr)
print (findCount(arr, n))
|
C#
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)
count++;
if (count % 2 == 0)
return 0;
else
return 1;
}
static public void Main ()
{
int [] arr = {1, 2, 4, 5, 1};
int n = arr.Length;
Console.WriteLine(findCount(arr, n));
}
}
|
PHP
<?php
function findCount( $arr , $n )
{
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] % 2 == 1)
$count ++;
if ( $count % 2 == 0)
return 0;
else
return 1;
}
$arr = array (1, 2, 4, 5, 1);
$n = 5;
echo findCount( $arr , $n );
?>
|
Javascript
<script>
function findCount( arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
if (arr[i] % 2 == 1)
count++;
if (count % 2 == 0)
return 0;
else
return 1;
}
let arr = [1, 2, 4, 5, 1];
let n = arr.length;
document.write(findCount(arr,n));
</script>
|
Time Complexity: O(n), where n is the number of elements in the array.
Auxiliary Space: O(1) as constant space for variables is being used
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...