Skip to content
Related Articles

Related Articles

Count of ways to make Array sum even by removing only one element
  • Difficulty Level : Easy
  • Last Updated : 10 Sep, 2020

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:
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:
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:

  1. 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[].
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

3

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :