- 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++
- Convert the array such that the GCD of the array becomes 1 in C++
- Sort the numbers so that the even numbers are ahead JavaScript
- Ways to remove one element from a binary string so that XOR becomes zero in C++
- Minimum flips in two binary arrays so that their XOR is equal to another array in C++.
- How to rescale a continuous variable so that the range of the rescale becomes 0 to 1 in R?
- Minimum toggles to partition a binary array so that it has first 0s then 1s in C++
- Finding sum of digits of a number until sum becomes single digit in C++
- How to get the sum of the numbers in even positions in an array?
- C++ program to find sum of digits of a number until sum becomes single digit
- Minimum number of swaps required to sort an array in C++
- Even Number With Prime Sum

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

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.

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

#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"; }

Minimum 1 should be added

Advertisements