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

王林
Release: 2023-08-25 17:41:06
forward
1154 people have browsed it

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;
}
Copy after login

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 < /strong>- 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;
}
Copy after login

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

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;
}
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template