Given an array **arr[]** of integers, the task is to check if it is possible to obtain a subsequence of **K** elements from the array such that their sum is **odd**. If it is possible, print **Yes**. Otherwise, print **No**.

**Examples:**

Input:arr[] = { 2, 5, 6, 7, 4 }, K = 3

Output:Yes

Explanation:

Subsequences {2, 5, 6}, {2, 6, 7} and {2, 7, 4} have odd sum

Input:arr[] = { 1, 5, 7, 11 }, K = 4

Output:No

Explanation:

Only subsequence of length 4 is {1, 5, 7, 11} which has even sum (24). Hence no such subsequence exists.

**Naive Approach:**

The simplest method to solve the problem is to generate all subsequences of length **K** and check if any of these subsequences have an odd sum. The time complexity for such an approach will be exponential and thus inefficient.

**Efficient Approach:**

The efficient method to approach the above problem will be to** count the number of odd elements** in the array and then, simply checking for all the edge cases when it will not be possible to find a subsequence with odd sum.

The edge cases to be considered when such a subsequence cannot be generated are as follows:

- If there are no odd elements in the array, any subsequence will contain only even elements and an even sum will be obtained. So, it is not possible to generate a subsequence with odd sum.
- If
**K**is even and there are no even elements present in the array, a subsequence with odd sum is not possible.

For all other cases, it will be possible to generate a subsequence with an odd sum.

Below is the implementation of above approach:

## C++

`// C++ program to check if a ` `// subsequence of length K ` `// with odd sum exists in the ` `// given array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if any required ` `// subsequence exists or not ` `bool` `isSubseqPossible(` `int` `arr[], ` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `i; ` ` ` `// Store count of odd and ` ` ` `// even elements in the array ` ` ` `int` `odd = 0, even = 0; ` ` ` ` ` `// Calculate the count of ` ` ` `// odd and even elements ` ` ` `for` `(i = 0; i < N; i++) { ` ` ` `if` `(arr[i] % 2 == 1) ` ` ` `odd++; ` ` ` `else` ` ` `even++; ` ` ` `} ` ` ` ` ` `// If no odd elements exists ` ` ` `// or no even elements exists ` ` ` `// when K is even ` ` ` `if` `(odd == 0 ` `|| (even == 0 && K % 2 == 0)) ` ` ` ` ` `// Subsequence is not possible ` ` ` `return` `false` `; ` ` ` ` ` `// Possible otherwise ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 3, 5, 7, 4 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `K = 3; ` ` ` `cout << (isSubseqPossible(arr, N, K) ` ` ` `? ` `"Yes"` ` ` `: ` `"No"` `); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if a ` `// subsequence of length K ` `// with odd sum exists in the ` `// given array ` `class` `GFG{ ` ` ` `// Function to check if any required ` `// subsequence exists or not ` `static` `boolean` `isSubseqPossible(` `int` `[]arr, ` ` ` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `i; ` ` ` ` ` `// Store count of odd and ` ` ` `// even elements in the array ` ` ` `int` `odd = ` `0` `, even = ` `0` `; ` ` ` ` ` `// Calculate the count of ` ` ` `// odd and even elements ` ` ` `for` `(i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % ` `2` `== ` `1` `) ` ` ` `odd++; ` ` ` `else` ` ` `even++; ` ` ` `} ` ` ` ` ` `// If no odd elements exists ` ` ` `// or no even elements exists ` ` ` `// when K is even ` ` ` `if` `(odd == ` `0` `|| (even == ` `0` `&& K % ` `2` `== ` `0` `)) ` ` ` ` ` `// Subsequence is not possible ` ` ` `return` `false` `; ` ` ` ` ` `// Possible otherwise ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `[]arr = { ` `2` `, ` `3` `, ` `5` `, ` `7` `, ` `4` `}; ` ` ` `int` `N = arr.length; ` ` ` `int` `K = ` `3` `; ` ` ` `System.out.print(isSubseqPossible(arr, N, K) ? ` ` ` `"Yes"` `: ` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check if a subsequence ` `# of length K with odd sum exists in the ` `# given array ` ` ` `# Function to check if any required ` `# subsequence exists or not ` `def` `isSubseqPossible(arr, N, K): ` ` ` ` ` `i ` `=` `0` ` ` ` ` `# Store count of odd and ` ` ` `# even elements in the array ` ` ` `odd ` `=` `0` ` ` `even ` `=` `0` ` ` ` ` `# Calculate the count of ` ` ` `# odd and even elements ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `if` `(arr[i] ` `%` `2` `=` `=` `1` `): ` ` ` `odd ` `+` `=` `1` ` ` `else` `: ` ` ` `even ` `+` `=` `1` ` ` ` ` `# If no odd element exists or no ` ` ` `# even element exists when K even ` ` ` `if` `(odd ` `=` `=` `0` `or` `(even ` `=` `=` `0` `and` `K ` `%` `2` `=` `=` `0` `)): ` ` ` ` ` `# Subsequence is not possible ` ` ` `return` `False` ` ` ` ` `# Otherwise possible ` ` ` `return` `True` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[ ` `2` `, ` `3` `, ` `5` `, ` `7` `, ` `4` `] ` ` ` `N ` `=` `len` `(arr) ` ` ` `K ` `=` `3` ` ` ` ` `print` `(` `"Yes"` `if` `isSubseqPossible(arr, N, K) ` `else` `"No"` `) ` ` ` `# This code is contributed by himanshu77 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if a ` `// subsequence of length K ` `// with odd sum exists in the ` `// given array ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to check if any required ` `// subsequence exists or not ` `static` `bool` `isSubseqPossible(` `int` `[]arr, ` ` ` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `i; ` ` ` ` ` `// Store count of odd and ` ` ` `// even elements in the array ` ` ` `int` `odd = 0, even = 0; ` ` ` ` ` `// Calculate the count of ` ` ` `// odd and even elements ` ` ` `for` `(i = 0; i < N; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % 2 == 1) ` ` ` `odd++; ` ` ` `else` ` ` `even++; ` ` ` `} ` ` ` ` ` `// If no odd elements exists ` ` ` `// or no even elements exists ` ` ` `// when K is even ` ` ` `if` `(odd == 0 || (even == 0 && K % 2 == 0)) ` ` ` ` ` `// Subsequence is not possible ` ` ` `return` `false` `; ` ` ` ` ` `// Possible otherwise ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 2, 3, 5, 7, 4 }; ` ` ` `int` `N = arr.Length; ` ` ` `int` `K = 3; ` ` ` `Console.Write(isSubseqPossible(arr, N, K) ? ` ` ` `"Yes"` `: ` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

Yes

** Time Complexity: **O(N)

** Auxiliary Space: **O(1)

## Recommended Posts:

- Check whether a subsequence exists with sum equal to k if arr[i]> 2*arr[i-1]
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest subsequence whose XOR value is odd
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Subsequence with maximum odd sum
- Check if an array has some palindromic subsequence of length at least 3
- Maximum Sum Subsequence of length k
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if a subarray exists with sum greater than the given Array
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Find the equal pairs of subsequence of S and subsequence of T
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Create an array of size N with sum S such that no subarray exists with sum S or S-K
- Longest Increasing Odd Even Subsequence
- Maximum length L such that the sum of all subarrays of length L is less than K

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.