- 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

# Add minimum number to an array so that the sum becomes even in C++?

Suppose there is an array with some numbers. We have to tell minimum how many numbers will be added with it to make the sum of the elements even. The number must be greater than 0. So if the sum of the elements is odd, we will add 1, but if the sum is already even, then we will add 2 with it to make it even.

## Algorithm

### addMinNumber(arr)

begin s := 0 for each element e from arr, do s := e + s done if s is even, then return 2, otherwise 1 end

## Example

#include<iostream> using namespace std; int addMinNumber(int arr[], int n) { int sum = 0; for(int i = 0; i<n; i++) { sum += arr[i]; } return (sum % 2)? 1 : 2; } main() { int arr[] = {5, 8, 4, 7, 5}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Minimum " << addMinNumber(arr, n) << " should be added"; }

## Output

Minimum 1 should be added

- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C programming
- Find the minimum value to be added so that array becomes balanced in C++
- Find minimum value to assign all array elements so that array product becomes greater in C++
- Minimum removals to make array sum even in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- First element that appears even number of times in an array in C++
- Convert the array such that the GCD of the array becomes 1 in C++
- Ways to remove one element from a binary string so that XOR becomes zero in C++
- Sort the numbers so that the even numbers are ahead JavaScript
- Minimum flips in two binary arrays so that their XOR is equal to another array in C++.
- Minimum toggles to partition a binary array so that it has first 0s then 1s in C++
- How to rescale a continuous variable so that the range of the rescale becomes 0 to 1 in R?
- Finding sum of digits of a number until sum becomes single digit in C++
- C++ program to find sum of digits of a number until sum becomes single digit
- How to get the sum of the numbers in even positions in an array?

Advertisements