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++
// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
// Function to find a number of ways
// to make array element sum even by
// removing one element
int find_num_of_ways(int arr[], int N)
{
int count_even = 0, count_odd = 0;
// Finding the count of even
// and odd elements
for (int i = 0; i < N; i++) {
if (arr[i] % 2) {
count_odd++;
}
else {
count_even++;
}
}
// If count_odd is odd then
// no. of ways is count_odd
if (count_odd % 2) {
return count_odd;
}
// Else no. of ways is count_even
else {
return count_even;
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 1, 3, 3, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << find_num_of_ways(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find a number of ways
// to make array element sum even by
// removing one element
static int find_num_of_ways(int arr[], int N)
{
int count_even = 0, count_odd = 0;
// Finding the count of even
// and odd elements
for(int i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
count_odd++;
}
else
{
count_even++;
}
}
// If count_odd is odd then
// no. of ways is count_odd
if (count_odd % 2 == 1)
{
return count_odd;
}
// Else no. of ways is count_even
else
{
return count_even;
}
}
// Driver Code
public static void main (String[] args)
{
// Given array arr[]
int arr[] = { 1, 3, 3, 2 };
int N = 4;
// Function call
System.out.print(find_num_of_ways(arr, N));
}
}
// This code is contributed by Ritik Bansal
Python3
# Python3 program for the above approach
# Function to find a number of ways
# to make array element sum even by
# removing one element
def find_num_of_ways(arr, N):
count_even = 0
count_odd = 0
# Finding the count of even
# and odd elements
for i in range(N):
if (arr[i] % 2):
count_odd += 1
else:
count_even += 1
# If count_odd is odd then
# no. of ways is count_odd
if (count_odd % 2):
return count_odd
# Else no. of ways is count_even
else:
return count_even
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 3, 3, 2 ]
N = len(arr)
# Function call
print(find_num_of_ways(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find a number of ways
// to make array element sum even by
// removing one element
static int find_num_of_ways(int []arr, int N)
{
int count_even = 0, count_odd = 0;
// Finding the count of even
// and odd elements
for(int i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
count_odd++;
}
else
{
count_even++;
}
}
// If count_odd is odd then
// no. of ways is count_odd
if (count_odd % 2 == 1)
{
return count_odd;
}
// Else no. of ways is count_even
else
{
return count_even;
}
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 1, 3, 3, 2 };
int N = 4;
// Function call
Console.Write(find_num_of_ways(arr, N));
}
}
// This code is contributed by Rutvik
JavaScript
<script>
// javascript program for the above approach
// Function to find a number of ways
// to make array element sum even by
// removing one element
function find_num_of_ways(arr , N)
{
var count_even = 0, count_odd = 0;
// Finding the count of even
// and odd elements
for(i = 0; i < N; i++)
{
if (arr[i] % 2 == 1)
{
count_odd++;
}
else
{
count_even++;
}
}
// If count_odd is odd then
// no. of ways is count_odd
if (count_odd % 2 == 1)
{
return count_odd;
}
// Else no. of ways is count_even
else
{
return count_even;
}
}
// Driver Code
// Given array arr
var arr = [ 1, 3, 3, 2 ];
var N = 4;
// Function call
document.write(find_num_of_ways(arr, N));
// This code is contributed by Amit Katiyar
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count of Ways to obtain given Sum from the given Array elements Given an array arr[], consisting of N non-negative integers and an integer S, the task is to find the number of ways to obtain the sum S by adding or subtracting array elements. Note: All the array elements need to be involved in generating the sum. Examples: Input: arr[] = {1, 1, 1, 1, 1}, S = 3 Ou
15+ min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element Given an array arr[] of length N, the task is to find the count of array indices such that removing an element from these indices makes the Bitwise xor of odd-indexed elements and even-indexed (1-based indexing) elements are equal. Examples: Input: arr[] = {1, 0, 1, 0, 1}, N = 5Output: 3Explanation:
10 min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Construct sum-array with sum of elements in given range You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {
7 min read
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once Given an array arr[] consisting of N integers, the task is to count the number of ways to split the array into two subarrays of equal sum after changing a single array element to 0. Examples: Input: arr[] = {1, 2, -1, 3}Output: 4Explanation: Replacing arr[0] by 0, arr[] is modified to {0, 2, -1, 3}.
11 min read