Given an array arr[], the task is to check if it is possible to make the sum of array odd such that any two indices i and j can be chosen and arr[i] can be set equal to arr[j] given that i != j.
Examples:
Input: arr[] = { 5, 4, 4, 5, 1, 3 }
Output: Yes
Explanation:
The sum of the array is 22. Put arr[0] = arr[1] where i=0 and j=1. The sum of this new array is 21 which is odd.Input: arr[] = { 2, 2, 8, 8 }
Output: No
Explanation:
Sum of the array is 20.
The sum of the array cannot change to odd as all the elements are even. Adding even number to an even number always gives an even result.
Approach: The idea is to find the sum all the elements in the array and simultaneously checking the number of even and odd numbers present in the array. If the sum is even, then one of the even numbers can be replaced with an odd number thereby making the sum of the array odd. If there are no odd elements in the array, then the sum of the array cannot be made odd.
Below is the implementation of the above approach:
C++
// C++ program to check if the array // with odd sum is possible #include <bits/stdc++.h> using namespace std; // Function to check if the // sum of the array can be made odd. bool isOdd(int arr[], int n) { int l, r, flag = 0, flag1 = 0, sum = 0; // Find sum of all elements and increment // check for odd or even elements in the array // so that by changing ai=aj, // the sum of the array can be made odd for (int i = 0; i < n; i++) { sum += arr[i]; if (arr[i] % 2 == 0 && flag == 0) { flag = 1; l = arr[i]; } if (arr[i] % 2 != 0 && flag1 == 0) { r = arr[i]; flag1 = 1; } } // If the sum is already odd if (sum % 2 != 0) { return true; } // Else, then both the flags should be checked. // Here, flag1 and flag represent if there is // an odd-even pair which can be replaced. else { if (flag1 == 1 && flag == 1) return true; else return false; } } // Driver code int main() { int ar[] = { 5, 4, 4, 5, 1, 3 }; int n = sizeof(ar) / sizeof(ar[0]); bool res = isOdd(ar, n); if (res) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Java
// Java program to check if the array // with odd sum is possible class GFG { // Function to check if the // sum of the array can be made odd. static boolean isOdd(int []arr, int n) { int l, r, flag = 0, flag1 = 0, sum = 0; // Find sum of all elements and increment // check for odd or even elements in the array // so that by changing ai=aj, // the sum of the array can be made odd for (int i = 0; i < n; i++) { sum += arr[i]; if (arr[i] % 2 == 0 && flag == 0) { flag = 1; l = arr[i]; } if (arr[i] % 2 != 0 && flag1 == 0) { r = arr[i]; flag1 = 1; } } // If the sum is already odd if (sum % 2 != 0) { return true; } // Else, then both the flags should be checked. // Here, flag1 and flag represent if there is // an odd-even pair which can be replaced. else { if (flag1 == 1 && flag == 1) return true; else return false; } } // Driver code public static void main (String[] args) { int ar[] = { 5, 4, 4, 5, 1, 3 }; int n = ar.length; boolean res = isOdd(ar, n); if (res == true) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 program to check if the array # with odd sum is possible # Function to check if the # sum of the array can be made odd. def isOdd(arr, n) : flag = 0; flag1 = 0; sum = 0; # Find sum of all elements and increment # check for odd or even elements in the array # so that by changing ai=aj, # the sum of the array can be made odd for i in range(n) : sum += arr[i]; if (arr[i] % 2 == 0 and flag == 0) : flag = 1; l = arr[i]; if (arr[i] % 2 != 0 and flag1 == 0) : r = arr[i]; flag1 = 1; # If the sum is already odd if (sum % 2 != 0) : return True; # Else, then both the flags should be checked. # Here, flag1 and flag represent if there is # an odd-even pair which can be replaced. else : if (flag1 == 1 and flag == 1) : return True; else : return False; # Driver code if __name__ == "__main__" : arr = [ 5, 4, 4, 5, 1, 3 ]; n = len(arr); res = isOdd(arr, n); if (res) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01 |
C#
// C# program to check if the array // with odd sum is possible using System; class GFG{ // Function to check if the // sum of the array can be made odd. static bool isOdd(int[] arr, int n) { int flag = 0, flag1 = 0, sum = 0; // Find sum of all elements and increment // check for odd or even elements in the array // so that by changing ai=aj, // the sum of the array can be made odd for (int i = 0; i < n; i++) { sum += arr[i]; if (arr[i] % 2 == 0 && flag == 0) { flag = 1; } if (arr[i] % 2 != 0 && flag1 == 0) { flag1 = 1; } } // If the sum is already odd if (sum % 2 != 0) { return true; } // Else, then both the flags should be checked. // Here, flag1 and flag represent if there is // an odd-even pair which can be replaced. else { if (flag1 == 1 && flag == 1) return true; else return false; } } // Driver code static public void Main () { int[] ar = { 5, 4, 4, 5, 1, 3 }; int n = ar.Length; bool res = isOdd(ar, n); if (res) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by shivanisingh |
Yes
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Make all the array elements odd with minimum operations of given type
- Find if it is possible to make all elements of an array equal by the given operations
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Check if it is possible to split given Array into K odd-sum subsets
- Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal
- Maximum possible Array sum after performing given operations
- Modify given array to make sum of odd and even indexed elements same
- Check if it is possible to make array increasing or decreasing by rotating the array
- Minimum insertions to make XOR of an Array equal to half of its sum
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Minimum pair sum operations to make array each element divisible by 4
- Check if it is possible to make array equal by doubling or tripling
- Minimum removals to make array sum odd
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Make all the elements of array even with given operations
- Minimum Cost to make all array elements equal using given operations
- Maximum Possible Product in Array after performing given Operations
- Maximum score possible after performing given operations on an Array
- Check if sum Y can be obtained from the Array by the given operations
- Count of Array elements greater than all elements on its left and next K elements on its right
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

