📌  相关文章
📜  查找是否可以选择包含恰好 K 个偶数的子数组

📅  最后修改于: 2021-09-04 08:29:49             🧑  作者: Mango

给定 2 个正整数NK以及一个数组arr[] ,任务是找出是否可以选择该数组的一个非空子数组,使得该子数组恰好包含K 个偶数整数。
例子:

方法:思路是计算数组中偶数的个数。现在可以有3种情况:

  • 如果数组中偶数的个数为0 (即数组中只有奇数,那么我们不能选择任何子数组
  • 如果数组中偶数的个数≥ K ,那么我们可以很容易地选择一个恰好有 K 个偶数的子数组
  • 否则不可能选择一个恰好包含 K 个偶数的子数组

下面是上述方法的实现:

CPP
// C++ program to check if it is possible to
// choose a subarray that contains exactly
// K even integers
#include 
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;
}


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


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


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


Javascript


输出:
Yes

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live