Home >Backend Development >C++ >In C programming, add the smallest number to an array such that the sum of the array becomes an even number

In C programming, add the smallest number to an array such that the sum of the array becomes an even number

王林
王林forward
2023-08-25 17:41:061137browse

In C programming, add the smallest number to an array such that the sum of the array becomes an even number

Given an array, add the smallest number (should be greater than 0) to the array so that the sum of the array becomes an even number.

Input- 1 2 3 4.

Output- 2

Explanation - Array The sum is 10, so we

add the smallest number 2 to make the sum even.

Method 1: Calculate the sum of all elements of the array, then check whether the sum is an even number, then add the minimum number of 2, otherwise add the minimum number of 1.

Input- 1 2 3 4,

Output- 2

Explanation-The sum of the array is 10, so we Add the smallest number 2 to make the sum even.

Example

#include<iostream>
using namespace std;
int main() {
   int arr[] = { 1, 2, 3, 4};
   int n=4;
   int sum=0;
   for (int i = 0; i <n; i++) {
      sum+=arr[i];
   }
   if (sum % 2==0) {
      cout <<"2";
   } else {
      cout <<"1";
   }
   return 0;
}

Method 2 - Calculate the count of odd elements in an array. If the odd number of occurrences is an even number, return 2, otherwise return 1.

Input - 1 2 3 4 5

Output - 1

Explanation- No. in the array is 3

Add the minimum number 1 to make the sum even.

Example

#include<iostream>
using namespace std;
int main() {
   int arr[] = { 1, 2, 3, 4,5};
   int n=5;
   int odd = 0;
   for (int i = 0; i < n; i++) {
      if (arr[i] % 2!=0) {
         odd += 1;
      }
   }
   if (odd % 2==0) {
      cout <<"2";
   } else {
      cout <<"1";
   }
   return 0;
}

Method 3 - Takes a flag variable (initialized to 0). Whenever we find an odd element in the array, we perform NOT(!) operation on the Boolean variable. This logical operator inverts the value of a flag variable (meaning if it is 0, convert the variable to 1 and vice versa).

Input- 1 2 3 4 5

Output- 1

Explanation-Variable initialization is 0.

Traverse the array p>

1 is an odd number, and the variable changes by 1.

2 is an even number

3 is an odd number, the variable changes to 0.

4 is an even number

4 p>

5 is an odd number, the variable becomes 1

If the variable value is 1, it means that there are an odd number of odd elements, so that The minimum number of elements whose sum is an even number is plus 1.

Otherwise the minimum quantity is 2.

Example

#include<iostream>
using namespace std;
int main() {
   int arr[] = { 1, 2, 3, 4,5};
   int n=5;
   bool odd = 0;
   for (int i = 0; i < n; i++) {
      if (arr[i] % 2!=0) {
         odd = !odd;
      }
   }
   if (odd) {
      cout <<"1";
   } else {
      cout <<"2";
   }
   return 0;
}

The above is the detailed content of In C programming, add the smallest number to an array such that the sum of the array becomes an even number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete