Check if K distinct array elements form an odd sum
Last Updated :
16 Oct, 2022
Given an array A[] of size N, the task is to check if it is possible to get odd sum using K distinct elements from the array.
Examples:
Input: N = 4, K = 2, A = {10, 19, 14, 14}
Output: YES
Explanation:
19 + 14 = 33, which is odd
Input: N = 3, K = 3, A = {101, 102, 103}
Output: NO
Explanation:
101 + 102 + 103 = 306, which is even
Approach:
- Let us look at some basic maths properties:
EVEN + EVEN = EVEN
ODD + ODD = EVEN
EVEN + ODD = ODD
- From the above properties, we can conclude that when an odd number of odd integers are added to any number of even integers, the result will always be odd.
Examples:
- 3 + 2 + 6 = 11 (odd)
- 1 + 3 + 7 + 4 = 15 (odd)
Hence, follow the steps below to solve the problem:
- Count distinct odd and even elements present in the array.
- If K or more odd elements are present in the array, print “Yes”.
- Otherwise, for every odd count of odd numbers, check if sufficient even numbers are present in the array or not.
- If any such case occurs, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool oddSum(vector< int >& A,
int N, int K)
{
set< int > Odd;
set< int > Even;
for ( int i = 0; i < N; i++) {
if (A[i] % 2 == 0) {
Even.insert(A[i]);
}
else {
Odd.insert(A[i]);
}
}
if (Odd.size() >= K)
return true ;
bool flag = false ;
for ( int i = 1; i < K; i += 2) {
int needed = K - i;
if (needed <= Even.size()) {
return true ;
}
}
return flag;
}
int main()
{
int N = 5;
vector< int > A = { 12, 1, 7, 7, 26, 18 };
int K = 3;
if (oddSum(A, N, K))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean oddSum( int []A,
int N, int K)
{
HashSet<Integer> Odd = new HashSet<Integer>();
HashSet<Integer> Even = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++)
{
if (A[i] % 2 == 0 )
{
Even.add(A[i]);
}
else
{
Odd.add(A[i]);
}
}
if (Odd.size() >= K)
return true ;
boolean flag = false ;
for ( int i = 1 ; i < K; i += 2 )
{
int needed = K - i;
if (needed <= Even.size())
{
return true ;
}
}
return flag;
}
public static void main(String[] args)
{
int N = 5 ;
int []A = { 12 , 1 , 7 , 7 , 26 , 18 };
int K = 3 ;
if (oddSum(A, N, K))
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def oddSum(A, N, K):
Odd = set ([])
Even = set ([])
for i in range (N):
if (A[i] % 2 = = 0 ):
Even.add(A[i])
else :
Odd.add(A[i])
if ( len (Odd) > = K):
return True
flag = False
for i in range ( 1 , K, 2 ):
needed = K - i
if (needed < = len (Even)):
return True
return flag
if __name__ = = "__main__" :
N = 5
A = [ 12 , 1 , 7 ,
7 , 26 , 18 ]
K = 3
if (oddSum(A, N, K)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool oddSum( int []A, int N, int K)
{
HashSet< int > Odd = new HashSet< int >();
HashSet< int > Even = new HashSet< int >();
for ( int i = 0; i < N; i++)
{
if (A[i] % 2 == 0)
{
Even.Add(A[i]);
}
else
{
Odd.Add(A[i]);
}
}
if (Odd.Count >= K)
return true ;
bool flag = false ;
for ( int i = 1; i < K; i += 2)
{
int needed = K - i;
if (needed <= Even.Count)
{
return true ;
}
}
return flag;
}
public static void Main(String[] args)
{
int N = 5;
int []A = { 12, 1, 7, 7, 26, 18 };
int K = 3;
if (oddSum(A, N, K))
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
function oddSum(A,N,K)
{
let Odd = new Set();
let Even = new Set();
for (let i = 0; i < N; i++)
{
if (A[i] % 2 == 0)
{
Even.add(A[i]);
}
else
{
Odd.add(A[i]);
}
}
if (Odd.size >= K)
return true ;
let flag = false ;
for (let i = 1; i < K; i += 2)
{
let needed = K - i;
if (needed <= Even.size)
{
return true ;
}
}
return flag;
}
let N = 5;
let A=[12, 1, 7, 7, 26, 18];
let K = 3;
if (oddSum(A, N, K))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time complexity: O(N)
Auxiliary space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...