Count of ways to make Array sum even by removing only one element
Last Updated :
22 Mar, 2021
Given an array arr[] positive integers, the task is to find the number of ways to convert the array sum even if we are allowed to remove only one element.
Examples:
Input: arr[] = { 1, 3, 3, 2 }
Output: 3
Explanation:
1. Remove 1, then sum is 3 + 3 + 2 = 8.
2. Remove 3, then sum is 1 + 3 + 2 = 6.
3. Remove 3, then sum is 1 + 3 + 2 = 6.
Input: arr[] = { 4, 8, 3, 3, 6 }
Output: 3
Explanation:
1. Remove 4, then sum is 8 + 3 + 3 + 6 = 20.
2. Remove 8, then sum is 4 + 3 + 3 + 6 = 16.
3. Remove 6, then sum is 4 + 8 + 3 + 3 = 18.
Approach: The key observation to the above problem statement is:
- If we have an odd number of odd elements then the sum is always odd then we have to remove one odd number from the array arr[] to make the sum even. Since we have to remove one element, therefore, the total number of ways of making the sum even is the count of odd elements in the array arr[].
- If we have an even number of odd elements then the sum is always even. Since we have to remove one element to make the sum even, therefore, the total number of ways of making the sum even is the count of even elements in the array arr[]
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int find_num_of_ways( int arr[], int N)
{
int count_even = 0, count_odd = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2) {
count_odd++;
}
else {
count_even++;
}
}
if (count_odd % 2) {
return count_odd;
}
else {
return count_even;
}
}
int main()
{
int arr[] = { 1, 3, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << find_num_of_ways(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int find_num_of_ways( int arr[], int N)
{
int count_even = 0 , count_odd = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] % 2 == 1 )
{
count_odd++;
}
else
{
count_even++;
}
}
if (count_odd % 2 == 1 )
{
return count_odd;
}
else
{
return count_even;
}
}
public static void main (String[] args)
{
int arr[] = { 1 , 3 , 3 , 2 };
int N = 4 ;
System.out.print(find_num_of_ways(arr, N));
}
}
|
Python3
def find_num_of_ways(arr, N):
count_even = 0
count_odd = 0
for i in range (N):
if (arr[i] % 2 ):
count_odd + = 1
else :
count_even + = 1
if (count_odd % 2 ):
return count_odd
else :
return count_even
if __name__ = = '__main__' :
arr = [ 1 , 3 , 3 , 2 ]
N = len (arr)
print (find_num_of_ways(arr, N))
|
C#
using System;
class GFG{
static int find_num_of_ways( int []arr, int N)
{
int count_even = 0, count_odd = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
count_odd++;
}
else
{
count_even++;
}
}
if (count_odd % 2 == 1)
{
return count_odd;
}
else
{
return count_even;
}
}
public static void Main( string [] args)
{
int []arr = { 1, 3, 3, 2 };
int N = 4;
Console.Write(find_num_of_ways(arr, N));
}
}
|
Javascript
<script>
function find_num_of_ways(arr , N)
{
var count_even = 0, count_odd = 0;
for (i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
count_odd++;
}
else
{
count_even++;
}
}
if (count_odd % 2 == 1)
{
return count_odd;
}
else
{
return count_even;
}
}
var arr = [ 1, 3, 3, 2 ];
var N = 4;
document.write(find_num_of_ways(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...