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
#include <bits/stdc++.h>
using namespace std;
void isPossible( int A[], int n, int k)
{
int countOfTwo = 0;
for ( int i = 0; i < n; i++) {
if (A[i] % 2 == 0) {
countOfTwo++;
}
}
if (k == 0 && countOfTwo == n)
cout << "NO\n" ;
else if (countOfTwo >= k) {
cout << "Yes\n" ;
}
else
cout << "No\n" ;
}
int main()
{
int arr[] = { 1, 2, 4, 5 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
isPossible(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void isPossible( int []A, int n, int k)
{
int countOfTwo = 0 ;
for ( int i = 0 ; i < n; i++) {
if (A[i] % 2 == 0 ) {
countOfTwo++;
}
}
if (k == 0 && countOfTwo == n)
System.out.print( "NO" );
else if (countOfTwo >= k) {
System.out.print( "YES" );
}
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int []arr = { 1 , 2 , 4 , 5 };
int K = 2 ;
int n = arr.length;
isPossible(arr, n, K);
}
}
|
Python3
def isPossible(A, n, k):
countOfTwo = 0
for i in range (n):
if (A[i] % 2 = = 0 ):
countOfTwo + = 1
if (k = = 0 and countOfTwo = = n):
print ( "NO\n" )
elif (countOfTwo > = k):
print ( "Yes\n" )
else :
print ( "No\n" )
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 5 ]
K = 2
N = len (arr)
isPossible(arr, N, K)
|
C#
using System;
class GFG{
static void isPossible( int []A, int n, int k)
{
int countOfTwo = 0;
for ( int i = 0; i < n; i++) {
if (A[i] % 2 == 0) {
countOfTwo++;
}
}
if (k == 0 && countOfTwo == n)
Console.Write( "NO" );
else if (countOfTwo >= k) {
Console.Write( "Yes" );
}
else
Console.Write( "No" );
}
public static void Main()
{
int []arr = { 1, 2, 4, 5 };
int K = 2;
int n = arr.Length;
isPossible(arr, n, K);
}
}
|
Javascript
<script>
function isPossible(A, n, k)
{
var countOfTwo = 0;
for ( var i = 0; i < n; i++) {
if (A[i] % 2 == 0) {
countOfTwo++;
}
}
if (k == 0 && countOfTwo == n)
document.write( "NO" );
else if (countOfTwo >= k) {
document.write( "Yes" );
}
else
document.write( "NO" );
}
var arr = [ 1, 2, 4, 5 ];
var K = 2;
var N = arr.length;
isPossible(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
24 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...