📌  相关文章
📜  检查是否存在长度为K且具有奇数和的子序列

📅  最后修改于: 2021-05-17 01:43:59             🧑  作者: Mango

给定整数数组arr [] ,任务是检查是否有可能从数组中获取K个元素的子序列,以使它们的和为奇数。如果可能,请打印“是” 。否则,打印No。

例子:

天真的方法:
解决该问题的最简单方法是生成所有长度为K的子序列,并检查这些子序列中是否有一个奇数和。这种方法的时间复杂度将是指数级的,因此效率很低。

高效方法:
解决上述问题的有效方法是对数组中奇数元素的数量进行计数,然后在不可能找到具有奇数和的子序列时,仅检查所有边缘情况。

无法生成这种子序列时要考虑的边缘情况如下:

  • 如果数组中没有奇数元素,则任何子序列将仅包含偶数元素,并且将获得偶数和。因此,不可能生成具有奇数和的子序列。
  • 如果K为偶数,并且数组中不存在偶数元素,则奇数和的子序列是不可能的。

对于所有其他情况,将可能生成具有奇数和的子序列。

下面是上述方法的实现:

C++
// C++ program to check if a
// subsequence of length K
// with odd sum exists in the
// given array
#include 
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;
}


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


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


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


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)