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 of 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++
#include <bits/stdc++.h>
using namespace std;
bool isOdd( int arr[], int n)
{
int l, r, flag = 0, flag1 = 0, sum = 0;
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 (sum % 2 != 0) {
return true ;
}
else {
if (flag1 == 1 && flag == 1)
return true ;
else
return false ;
}
}
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
import java.io.*;
class GFG {
static boolean isOdd( int []arr, int n)
{
int l, r, flag = 0 , flag1 = 0 , sum = 0 ;
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 (sum % 2 != 0 ) {
return true ;
}
else {
if (flag1 == 1 && flag == 1 )
return true ;
else
return false ;
}
}
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" );
}
}
|
Python3
def isOdd(arr, n) :
flag = 0 ; flag1 = 0 ; sum = 0 ;
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 ( sum % 2 ! = 0 ) :
return True ;
else :
if (flag1 = = 1 and flag = = 1 ) :
return True ;
else :
return False ;
if __name__ = = "__main__" :
arr = [ 5 , 4 , 4 , 5 , 1 , 3 ];
n = len (arr);
res = isOdd(arr, n);
if (res) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool isOdd( int [] arr, int n)
{
int flag = 0, flag1 = 0, sum = 0;
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 (sum % 2 != 0) {
return true ;
}
else {
if (flag1 == 1 && flag == 1)
return true ;
else
return false ;
}
}
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" );
}
}
|
Javascript
<script>
function isOdd(arr, n)
{
let l, r, flag = 0, flag1 = 0, sum = 0;
for (let 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 (sum % 2 != 0) {
return true ;
}
else {
if (flag1 == 1 && flag == 1)
return true ;
else
return false ;
}
}
let ar = [ 5, 4, 4, 5, 1, 3 ];
let n = ar.length;
let res = isOdd(ar, n);
if (res == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.