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++
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return if
// odd sum is possible or not
bool oddSum(vector<int>& A,
int N, int K)
{
// Stores distinct odd elements
set<int> Odd;
// Stores distinct even elements
set<int> Even;
// Iterating through given array
for (int i = 0; i < N; i++) {
// If element is even
if (A[i] % 2 == 0) {
Even.insert(A[i]);
}
// If element is odd
else {
Odd.insert(A[i]);
}
}
// If atleast K elements
// in the array are odd
if (Odd.size() >= K)
return true;
bool flag = false;
// Check for all odd frequencies
// of odd elements whether
// sufficient even numbers
// are present or not
for (int i = 1; i < K; i += 2) {
// Count of even numbers
// required
int needed = K - i;
// If required even numbers
// are present in the array
if (needed <= Even.size()) {
return true;
}
}
return flag;
}
// Driver Program
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
// Java program for the above approach
import java.util.*;
class GFG{
// Function to return if
// odd sum is possible or not
static boolean oddSum(int []A,
int N, int K)
{
// Stores distinct odd elements
HashSet<Integer> Odd = new HashSet<Integer>();
// Stores distinct even elements
HashSet<Integer> Even = new HashSet<Integer>();
// Iterating through given array
for(int i = 0; i < N; i++)
{
// If element is even
if (A[i] % 2 == 0)
{
Even.add(A[i]);
}
// If element is odd
else
{
Odd.add(A[i]);
}
}
// If atleast K elements
// in the array are odd
if (Odd.size() >= K)
return true;
boolean flag = false;
// Check for all odd frequencies
// of odd elements whether
// sufficient even numbers
// are present or not
for(int i = 1; i < K; i += 2)
{
// Count of even numbers
// required
int needed = K - i;
// If required even numbers
// are present in the array
if (needed <= Even.size())
{
return true;
}
}
return flag;
}
// Driver code
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");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program for
# the above approach
# Function to return if
# odd sum is possible or not
def oddSum(A, N, K):
# Stores distinct odd elements
Odd = set([])
# Stores distinct even elements
Even = set([])
# Iterating through given array
for i in range(N):
# If element is even
if (A[i] % 2 == 0):
Even.add(A[i])
# If element is odd
else:
Odd.add(A[i])
# If atleast K elements
# in the array are odd
if (len(Odd) >= K):
return True
flag = False
# Check for all odd frequencies
# of odd elements whether
# sufficient even numbers
# are present or not
for i in range(1, K, 2):
# Count of even numbers
# required
needed = K - i
# If required even numbers
# are present in the array
if (needed <= len(Even)):
return True
return flag
# Driver Program
if __name__ == "__main__":
N = 5
A = [12, 1, 7,
7, 26, 18]
K = 3
if (oddSum(A, N, K)):
print("YES")
else:
print("NO")
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return if
// odd sum is possible or not
static bool oddSum(int []A, int N, int K)
{
// Stores distinct odd elements
HashSet<int> Odd = new HashSet<int>();
// Stores distinct even elements
HashSet<int> Even = new HashSet<int>();
// Iterating through given array
for(int i = 0; i < N; i++)
{
// If element is even
if (A[i] % 2 == 0)
{
Even.Add(A[i]);
}
// If element is odd
else
{
Odd.Add(A[i]);
}
}
// If atleast K elements
// in the array are odd
if (Odd.Count >= K)
return true;
bool flag = false;
// Check for all odd frequencies
// of odd elements whether
// sufficient even numbers
// are present or not
for(int i = 1; i < K; i += 2)
{
// Count of even numbers
// required
int needed = K - i;
// If required even numbers
// are present in the array
if (needed <= Even.Count)
{
return true;
}
}
return flag;
}
// Driver code
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");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript program for the above approach
// Function to return if
// odd sum is possible or not
function oddSum(A,N,K)
{
// Stores distinct odd elements
let Odd = new Set();
// Stores distinct even elements
let Even = new Set();
// Iterating through given array
for(let i = 0; i < N; i++)
{
// If element is even
if (A[i] % 2 == 0)
{
Even.add(A[i]);
}
// If element is odd
else
{
Odd.add(A[i]);
}
}
// If atleast K elements
// in the array are odd
if (Odd.size >= K)
return true;
let flag = false;
// Check for all odd frequencies
// of odd elements whether
// sufficient even numbers
// are present or not
for(let i = 1; i < K; i += 2)
{
// Count of even numbers
// required
let needed = K - i;
// If required even numbers
// are present in the array
if (needed <= Even.size)
{
return true;
}
}
return flag;
}
// Driver code
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");
// This code is contributed by avanitrachhadiya2155
</script>
Time complexity: O(N)
Auxiliary space: O(N)
Similar Reads
Sum of all odd frequency elements in an array Given an array of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given array. That is the sum of all such elements whose frequency is odd in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3} Output : 9 The odd occurring element is 3,
8 min read
Sum of all odd frequency elements in an array Given an array of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given array. That is the sum of all such elements whose frequency is odd in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3} Output : 9 The odd occurring element is 3,
8 min read
Difference between sum of odd and even frequent elements in an Array Given an array arr[] of integers, the task is to find the absolute difference between the sum of all odd frequent array elements and the sum of all even frequent array elements. Examples: Input: arr[] = {1, 5, 5, 2, 4, 3, 3} Output: 9 Explanation: The even frequent elements are 5 and 3 (both occurri
12 min read
Difference between sum of K maximum even and odd array elements Given an array arr[] and a number K, the task is to find the absolute difference of the sum of K maximum even and odd array elements.Note: At least K even and odd elements are present in the array respectively. Examples: Input arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output: 2Explanation:The 2 maximum even
8 min read
Sum of distinct elements when elements are in range 1 to n Given an array of n elements such that every element of the array is an integer in the range 1 to n, find the sum of all the distinct elements of the array. Examples: Input: arr[] = {5, 1, 2, 4, 6, 7, 3, 6, 7} Output: 28 The distinct elements in the array are 1, 2, 3, 4, 5, 6, 7 Input: arr[] = {1, 1
5 min read
Check if sum of exactly K elements of the Array can be odd or not Given an array, arr[] and an integer K. Check whether it is possible to get an odd sum by choosing exactly K elements of the array. Examples: Input: arr[] = {1, 2, 3}, K = 2 Output: Possible Explanation: {2, 3} â¾ 2 + 3 = 5 Input: arr[] = {2, 2, 4, 2}, K = 4 Output: Not Possible Explanation: {2, 2, 4
10 min read