- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Minimum removals to make array sum even in C++

## Problem statement

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.

## Example

If input array is {10, 20, 30, 5} then we need to remove one element i.e. 5 to make array sum even

## Algorithm

1. Sum of any number of even numbers is always even 2. Sum of odd numbers of odd numbers is always odd 3. Sum of odd numbers of even times is always even 4. 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

## Example

#include <bits/stdc++.h> using namespace std; int getMinRemovals(int *arr, int n) { int cnt = 0; for (int i = 0; i < n; ++i) { if (arr[i] % 2 == 1) { ++cnt; } } return (cnt % 2 == 0) ? 0 : 1; } int main() { int arr[] = {10, 20, 30, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum required removals = " << getMinRemovals(arr, n) << endl; return 0; }

When you compile and execute above program. It generates following output

## Output

Minimum required removals = 1

- Related Questions & Answers
- Minimum removals to make array sum odd in C++
- Minimum removals from array to make GCD Greater in Python
- Minimum removals from array to make GCD Greater in C++
- Minimum removals from array to make max – min <= K in C++
- Add minimum number to an array so that the sum becomes even in C++?
- Add minimum number to an array so that the sum becomes even in C programming
- Minimum Increment to Make Array Unique in C++
- Minimum operations to make XOR of array zero in C++
- Minimum insertions to make a Co-prime array in C++
- Finding minimum steps to make array elements equal in JavaScript
- Program to find minimum moves to make array complementary in Python
- Minimum operation to make all elements equal in array in C++
- Find minimum operations needed to make an Array beautiful in C++
- Program to find minimum operations to make array equal using Python
- Program to find minimum operations needed to make two arrays sum equal in Python

Advertisements