Add minimum number to an array so that the sum becomes even
Last Updated : 19 Sep, 2023
Given an array, write a program to add the minimum number(should be greater than 0) to the array so that the sum of array becomes even.
Examples:
Input : 1 2 3 4 5 6 7 8 Output : 2 Explanation : Sum of array is 36, so we add minimum number 2 to make the sum even. Input : 1 2 3 4 5 6 7 8 9 Output : 1
Method 1 (Computing Sum). We calculate the sum of all elements of the array, then we can check if the sum is even minimum number is 2, else minimum number is 1. This method can cause overflow if sum exceeds allowed limit.
Method 2. Instead of calculating the sum of numbers, we keep the count of odd number of elements in the array. If count of odd numbers present is even we return 2, else we return 1.
For example - Array contains : 1 2 3 4 5 6 7
Odd number counts is 4. And we know that the sum of even numbers of odd number is even. And sum of even number is always even (that is why, we don't keep count of even numbers).
Implementation:
C++ // CPP program to add minimum number // so that the sum of array becomes even #include <iostream> using namespace std; // Function to find out minimum number int minNum(int arr[], int n) { // Count odd number of terms in array int odd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2) odd += 1; return (odd % 2)? 1 : 2; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); cout << minNum(arr, n) << "n"; return 0; }
Java // Java program to add minimum number // so that the sum of array becomes even class GFG { // Function to find out minimum number static int minNum(int arr[], int n) { // Count odd number of terms in array int odd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 != 0) odd += 1; return ((odd % 2) != 0)? 1 : 2; } // Driver method to test above function public static void main(String args[]) { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int n = arr.length; System.out.println(minNum(arr, n)); } }
Python3 # Python program to add minimum number # so that the sum of array becomes even # Function to find out minimum number def minNum(arr, n): # Count odd number of terms in array odd = 0 for i in range(n): if (arr[i] % 2): odd += 1 if (odd % 2): return(1) return (2) # Driver code arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] n = len(arr) print(minNum(arr, n))
C# // C# program to add minimum number // so that the sum of array becomes even using System; class GFG { // Function to find out minimum number static int minNum(int []arr, int n) { // Count odd number of terms in array int odd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 != 0) odd += 1; return ((odd % 2) != 0)? 1 : 2; } // Driver Code public static void Main() { int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = arr.Length; Console.Write(minNum(arr, n)); } } // This code is contributed by Nitin Mittal.
PHP <?php // PHP program to add minimum number // so that the sum of array becomes even // Function to find out minimum number function minNum( $arr, $n) { // Count odd number of // terms in array $odd = 0; for ($i = 0; $i < $n; $i++) if ($arr[$i] % 2) $odd += 1; return ($odd % 2)? 1 : 2; } // Driver code $arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $n = count($arr); echo minNum($arr, $n) ; // This code is contributed by anuj_67. ?>
JavaScript <script> // Javascript program to add minimum number // so that the sum of array becomes even // Function to find out minimum number function minNum(arr, n) { // Count odd number of terms in array let odd = 0; for (let i = 0; i < n; i++) if (arr[i] % 2 != 0) odd += 1; return ((odd % 2) != 0)? 1 : 2; } // driver program let arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; let n = arr.length; document.write(minNum(arr, n)); // This code is contributed by code_hunt. </script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3. We can also improve the 2 method, we don't need to keep count of number of odd elements present. We can take a boolean variable (initialized as 0). Whenever we find the odd element in the array we perform the NOT(!) operation on the boolean variable. This logical operator inverts the value of the boolean variable (meaning if it is 0, it converts the variable to 1 and vice-versa).
For example - Array contains : 1 2 3 4 5
Explanation : variable initialized as 0.
Traversing the array
- 1 is odd, applying NOT operation in variable, now variable becomes 1.
- 2 is even, no operation.
- 3 is odd, applying NOT operation in variable, now variable becomes 0.
- 4 is even, no operation.
- 5 is odd, applying NOT operation in variable, now variable becomes 1.
If variable value is 1 it means odd number of odd elements are present, minimum number to make sum of elements even is by adding 1.
Else minimum number is 2.
Implementation:
C++ // CPP program to add minimum number // so that the sum of array becomes even #include <iostream> using namespace std; // Function to find out minimum number int minNum(int arr[], int n) { // Count odd number of terms in array bool odd = 0; for (int i = 0; i < n; i++) if (arr[i] % 2) odd = !odd; if (odd) return 1; return 2; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); cout << minNum(arr, n) << "n"; return 0; }
Java // Java program to add minimum number // so that the sum of array becomes even class GFG { // Function to find out minimum number static int minNum(int arr[], int n) { // Count odd number of terms in array Boolean odd = false; for (int i = 0; i < n; i++) if (arr[i] % 2 != 0) odd = !odd; if (odd) return 1; return 2; } //Driver method to test above function public static void main(String args[]) { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int n = arr.length; System.out.println(minNum(arr, n)); } }
Python3 # Python program to add minimum number # so that the sum of array becomes even # Function to find out minimum number def minNum(arr, n): # Count odd number of terms in array odd = False for i in range(n): if (arr[i] % 2): odd = not odd if (odd): return 1 return 2 # Driver code arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] n = len(arr) print (minNum(arr, n))
C# // C# program to add minimum number // so that the sum of array becomes even using System; class GFG { // Function to find out minimum number static int minNum(int []arr, int n) { // Count odd number of terms in array bool odd = false; for (int i = 0; i < n; i++) if (arr[i] % 2 != 0) odd = !odd; if (odd) return 1; return 2; } //Driver Code public static void Main() { int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = arr.Length; Console.Write(minNum(arr, n)); } } // This code is contributed by Nitin Mittal.
PHP <?php // PHP program to add minimum number // so that the sum of array becomes even // Function to find out minimum number function minNum($arr, $n) { // Count odd number of // terms in array $odd = 0; for($i = 0; $i < $n; $i++) if ($arr[$i] % 2) $odd = !$odd; if ($odd) return 1; return 2; } // Driver code $arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $n = sizeof($arr); echo minNum($arr, $n) ,"\n"; // This code is contributed by nitin mittal ?>
JavaScript <script> // Javascript program to add minimum number // so that the sum of array becomes even // Function to find out minimum number function minNum(arr, n) { // Count odd number of terms in array let odd = false; for (let i = 0; i < n; i++) if (arr[i] % 2 != 0) odd = !odd; if (odd) return 1; return 2; } let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let n = arr.length; document.write(minNum(arr, n)); // This code is contributed by divyesh072019. </script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Exercise :
Find the minimum number required to make the sum of elements odd.
Similar Reads
Find the minimum value to be added so that array becomes balanced Given an array of even size, task is to find minimum value that can be added to an element so that array become balanced. An array is balanced if the sum of the left half of the array elements is equal to the sum of right half. Suppose, we have an array 1 3 1 2 4 3. The Sum of first three elements i
5 min read
Minimize adding odd and subtracting even numbers to make all array elements equal to K Given an array, arr[] of size N and an integer K, the task is to find the minimum number of operations required to make all array elements equal to K by performing the following operations any number of times: Convert arr[i] to arr[i] + X, where X is an odd number.Convert arr[i] to arr[i] - Y, where
7 min read
Minimum elements to be removed such that sum of adjacent elements is always even Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is even. Examples: Input : arr[] = {1, 2, 3} Output : 1 Remove 2 from the array. Input : arr[] = {1, 3, 5, 4, 2} Output : 2 Remove 4 and 2. Approa
4 min read
Find a number X such that XOR of given Array after adding X to each element is 0 Given an array arr[] of odd length N containing positive integers. The task is to find a positive integer X such that, adding X to all the elements of arr[] and then taking XOR of all the elements gives 0. Return -1 if no such X exists. Examples: Input: arr[] = {2, 4, 5}Output: 1Explanation: Followi
6 min read
Sum of all even occurring element in an array Given an array of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given array. That is the sum of all such elements whose frequency is even in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3}Output : 6The even occurring element are
12 min read
Rearrange array such that all even-indexed elements in the Array is even Given an array arr[], the task is to check if it is possible to rearrange the array in such a way that every even index(1-based indexing) contains an even number. If such a rearrangement is not possible, print "No". Otherwise, print "Yes" and print a possible arrangement Examples: Input: arr[] = {2,
6 min read