Minimum removals to make array sum odd
Last Updated :
23 May, 2022
Given an array Arr[] of N integers. The task is to find the minimum number of elements needed to be removed from the array so that the sum of the remaining elements is odd. Considering there is at least 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 the odd numbers of an odd number is always odd. So to make the array sum odd, the 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++
#include <iostream>
using namespace std;
int findCount( int arr[], int n)
{
int countOdd = 0;
for ( int i = 0; i < n; i++)
if (arr[i] % 2 == 1)
countOdd++;
if (countOdd % 2 == 0)
return 1;
else
return 0;
}
int main()
{
int arr[] = { 1, 2, 3, 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 countOdd = 0 ;
for ( int i = 0 ; i < n; i++)
if (arr[i] % 2 == 1 )
countOdd++;
if (countOdd % 2 == 0 )
return 1 ;
else
return 0 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 5 , 1 };
int n = arr.length;
System.out.println(findCount(arr, n));
}
}
|
Python3
def findCount(arr, n) :
countOdd = 0 ;
for i in range (n) :
if (arr[i] % 2 = = 1 ) :
countOdd + = 1 ;
if (countOdd % 2 = = 0 ) :
return 1 ;
else :
return 0 ;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 5 , 1 ];
n = len (arr) ;
print (findCount(arr, n));
|
C#
using System;
class GfG
{
static int findCount( int []arr, int n)
{
int countOdd = 0;
for ( int i = 0; i < n; i++)
if (arr[i] % 2 == 1)
countOdd++;
if (countOdd % 2 == 0)
return 1;
else
return 0;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 5, 1 };
int n = arr.Length;
Console.WriteLine(findCount(arr, n));
}
}
|
PHP
<?php
function findCount( $arr , $n )
{
$countOdd = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] % 2 == 1)
$countOdd ++;
if ( $countOdd % 2 == 0)
return 1;
else
return 0;
}
$arr = array (1, 2, 3, 5, 1);
$n = sizeof( $arr );
echo (findCount( $arr , $n ));
?>
|
Javascript
<script>
function findCount(arr, n)
{
var countOdd = 0;
for ( var i = 0; i < n; i++)
if (arr[i] % 2 == 1)
countOdd++;
if (countOdd % 2 == 0)
return 1;
else
return 0;
}
var arr = [ 1, 2, 3, 5, 1 ];
var n = arr.length;
document.write( findCount(arr, n));
</script>
|
Time Complexity: O(n), where n is the size of the array.
Auxiliary Space: O(1), no extra space required so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...