# Find if it is possible to choose subarray that it contains exactly K even integers

Given 2 positive integers **N** and **K** and an array **arr[]**, the task is to find whether it is possible to choose a non-empty subarray of the array such that the subarray contains exactly **K** even integers.

**Examples:**

Input:N = 4, K = 2, arr[] = {1, 2, 4, 5}

Output:Yes

Explanation:

We can select the subarray {2, 4} which contains exactly K = 2 even numbers.

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

Output:No

Explanation:

There are only two even numbers. Therefore, we cannot choose a subarray with K = 3 even numbers.

**Approach:** The idea is to count the number of even numbers in the array. Now there can be 3 cases:

- If the count of even numbers in the array is
**0**(i.e.) there are only odd numbers in the array, then we can’t select any subarray - If the count of even numbers in the array is
**≥ K**, then we can easily select a subarray with exactly K even integers - Else it is not possible to select a subarray with exactly K even integers

Below is the implementation of the above approach:

## CPP

`// C++ program to check if it is possible to ` `// choose a subarray that contains exactly ` `// K even integers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if it is possible to ` `// choose a subarray that contains exactly ` `// K even integers ` `void` `isPossible(` `int` `A[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Variable to store the count of ` ` ` `// even numbers ` ` ` `int` `countOfTwo = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(A[i] % 2 == 0) { ` ` ` `countOfTwo++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we have to select 0 even numbers ` ` ` `// but there is all odd numbers in the array ` ` ` `if` `(k == 0 && countOfTwo == n) ` ` ` `cout << ` `"NO\n"` `; ` ` ` ` ` `// If the count of even numbers is greater than ` ` ` `// or equal to K then we can select a ` ` ` `// subarray with exactly K even integers ` ` ` `else` `if` `(countOfTwo >= k) { ` ` ` `cout << ` `"Yes\n"` `; ` ` ` `} ` ` ` ` ` `// If the count of even numbers is less than K ` ` ` `// then we cannot select any subarray with ` ` ` `// exactly K even integers ` ` ` `else` ` ` `cout << ` `"No\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 4, 5 }; ` ` ` `int` `K = 2; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `isPossible(arr, N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if it is possible to ` `// choose a subarray that contains exactly ` `// K even integers ` `import` `java.util.*; ` ` ` `class` `GFG{ ` `// Function to check if it is possible to ` `// choose a subarray that contains exactly ` `// K even integers ` `static` `void` `isPossible(` `int` `[]A, ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Variable to store the count of ` ` ` `// even numbers ` ` ` `int` `countOfTwo = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `if` `(A[i] % ` `2` `== ` `0` `) { ` ` ` `countOfTwo++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we have to select 0 even numbers ` ` ` `// but there is all odd numbers in the array ` ` ` `if` `(k == ` `0` `&& countOfTwo == n) ` ` ` `System.out.print(` `"NO"` `); ` ` ` ` ` `// If the count of even numbers is greater than ` ` ` `// or equal to K then we can select a ` ` ` `// subarray with exactly K even integers ` ` ` `else` `if` `(countOfTwo >= k) { ` ` ` `System.out.print(` `"YES"` `); ` ` ` `} ` ` ` ` ` `// If the count of even numbers is less than K ` ` ` `// then we cannot select any subarray with ` ` ` `// exactly K even integers ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[]arr = { ` `1` `, ` `2` `, ` `4` `, ` `5` `}; ` ` ` `int` `K = ` `2` `; ` ` ` `int` `n = arr.length; ` ` ` ` ` `isPossible(arr, n, K); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check if it is possible to ` `# choose a subarray that contains exactly ` `# K even integers ` ` ` `# Function to check if it is possible to ` `# choose a subarray that contains exactly ` `# K even integers ` `def` `isPossible(A, n, k): ` ` ` ` ` `# Variable to store the count of ` ` ` `# even numbers ` ` ` `countOfTwo ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(A[i] ` `%` `2` `=` `=` `0` `): ` ` ` `countOfTwo ` `+` `=` `1` ` ` ` ` `# If we have to select 0 even numbers ` ` ` `# but there is all odd numbers in the array ` ` ` `if` `(k ` `=` `=` `0` `and` `countOfTwo ` `=` `=` `n): ` ` ` `print` `(` `"NO\n"` `) ` ` ` ` ` `# If the count of even numbers is greater than ` ` ` `# or equal to K then we can select a ` ` ` `# subarray with exactly K even integers ` ` ` `elif` `(countOfTwo >` `=` `k): ` ` ` `print` `(` `"Yes\n"` `) ` ` ` ` ` `# If the count of even numbers is less than K ` ` ` `# then we cannot select any subarray with ` ` ` `# exactly K even integers ` ` ` `else` `: ` ` ` `print` `(` `"No\n"` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr` `=` `[` `1` `, ` `2` `, ` `4` `, ` `5` `] ` ` ` `K ` `=` `2` ` ` `N ` `=` `len` `(arr) ` ` ` ` ` `isPossible(arr, N, K) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if it is possible to ` `// choose a subarray that contains exactly ` `// K even integers ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check if it is possible to ` `// choose a subarray that contains exactly ` `// K even integers ` `static` `void` `isPossible(` `int` `[]A, ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Variable to store the count of ` ` ` `// even numbers ` ` ` `int` `countOfTwo = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(A[i] % 2 == 0) { ` ` ` `countOfTwo++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If we have to select 0 even numbers ` ` ` `// but there is all odd numbers in the array ` ` ` `if` `(k == 0 && countOfTwo == n) ` ` ` `Console.Write(` `"NO"` `); ` ` ` ` ` `// If the count of even numbers is greater than ` ` ` `// or equal to K then we can select a ` ` ` `// subarray with exactly K even integers ` ` ` `else` `if` `(countOfTwo >= k) { ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `} ` ` ` ` ` `// If the count of even numbers is less than K ` ` ` `// then we cannot select any subarray with ` ` ` `// exactly K even integers ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 1, 2, 4, 5 }; ` ` ` `int` `K = 2; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `isPossible(arr, n, K); ` `} ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Longest Subarray of non-negative Integers
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find K consecutive integers such that their sum is N
- Find N distinct integers with sum N
- Find the prime P using given four integers
- Find N distinct integers with zero sum
- Find if there is a subarray with 0 sum
- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Find K distinct positive odd integers with sum N
- Find the largest interval that contains exactly one of the given N integers.
- Find three integers less than or equal to N such that their LCM is maximum
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find N integers with given difference between product and sum
- Find sum in range L to R in given sequence of integers
- Find array such that no subarray has xor zero or Y
- Find the subarray with least average
- Find n positive integers that satisfy the given equations
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find distinct integers for a triplet with given product

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.