Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Add minimum number to an array so that the sum becomes even in C programming
Given an array, we need to add the minimum positive number to make the sum of all array elements even. The key insight is that we only need to analyze the parity (odd/even nature) of numbers to determine the minimum addition required.
Syntax
int findMinimumToAdd(int arr[], int n);
Method 1: Calculate Total Sum
Calculate the sum of all elements in the array, then check if the sum is even. If the sum is already even, add 2 (minimum positive even number). If the sum is odd, add 1 to make it even −
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4};
int n = 4;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nSum of array: %d<br>", sum);
if (sum % 2 == 0) {
printf("Minimum number to add: 2<br>");
} else {
printf("Minimum number to add: 1<br>");
}
return 0;
}
Array elements: 1 2 3 4 Sum of array: 10 Minimum number to add: 2
Method 2: Count Odd Numbers
Count the number of odd elements in the array. If the count of odd numbers is even, we need to add 2. If the count is odd, we need to add 1 −
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int oddCount = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
oddCount++;
}
}
printf("Array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of odd elements: %d<br>", oddCount);
if (oddCount % 2 == 0) {
printf("Minimum number to add: 2<br>");
} else {
printf("Minimum number to add: 1<br>");
}
return 0;
}
Array elements: 1 2 3 4 5 Number of odd elements: 3 Minimum number to add: 1
Method 3: Toggle Flag Approach
Use a boolean flag initialized to 0. For each odd element encountered, toggle the flag using the NOT operator. This efficiently tracks the parity of odd numbers −
#include <stdio.h>
#include <stdbool.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
bool oddFlag = false;
printf("Array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
if (arr[i] % 2 != 0) {
oddFlag = !oddFlag;
printf("(odd, flag=%s) ", oddFlag ? "true" : "false");
}
}
printf("\nFinal flag value: %s<br>", oddFlag ? "true" : "false");
if (oddFlag) {
printf("Minimum number to add: 1<br>");
} else {
printf("Minimum number to add: 2<br>");
}
return 0;
}
Array elements: 1 (odd, flag=true) 2 3 (odd, flag=false) 4 5 (odd, flag=true) Final flag value: true Minimum number to add: 1
Key Points
- Method 3 is most efficient with O(1) space complexity compared to Method 1's potential overflow risk.
- The logic depends on the mathematical fact that odd + odd = even, and even + odd = odd.
- All methods have O(n) time complexity for array traversal.
Conclusion
To make an array sum even, add 1 if there's an odd number of odd elements, otherwise add 2. The toggle flag method is the most space-efficient approach for large arrays.
