📌  相关文章
📜  检查K个不同的数组元素是否形成奇数和

📅  最后修改于: 2021-06-26 16:41:17             🧑  作者: Mango

给定大小为N的数组A [] ,任务是检查是否有可能使用数组中的K个不同元素来获得奇数和。
例子:

方法:

  • 让我们看一些基本的数学属性:
EVEN + EVEN = EVEN
ODD  + ODD  = EVEN
EVEN + ODD  = ODD

  • 从以上属性,我们可以得出结论,当将奇数个奇数整数添加到任意数量的偶数整数时,结果将始终为奇数。

因此,请按照以下步骤解决问题:

  • 计算数组中存在的不同的奇数和偶数元素。
  • 如果数组中存在K个或更多奇数元素,则打印“是”。
  • 否则,对于奇数的每个奇数计数,检查数组中是否存在足够的偶数。
  • 如果出现这种情况,请打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ program for above approach
 
#include 
using namespace std;
 
// Function to return if
// odd sum is possible or not
bool oddSum(vector& A,
            int N, int K)
{
 
    // Stores distinct odd elements
    set Odd;
    // Stores distinct even elements
    set Even;
 
    // Iterating through given array
    for (int i = 0; i < N; i++) {
 
        // If element is even
        if (A[i] % 2 == 0) {
            Even.insert(A[i]);
        }
        // If element is odd
        else {
            Odd.insert(A[i]);
        }
    }
 
    // If atleast K elements
    // in the array are odd
    if (Odd.size() >= K)
        return true;
 
    bool flag = false;
 
    // Check for all odd frequencies
    // of odd elements whether
    // sufficient even numbers
    // are present or not
    for (int i = 1; i < K; i += 2) {
 
        // Count of even numbers
        // required
        int needed = K - i;
 
        // If required even numbers
        // are present in the array
        if (needed <= Even.size()) {
 
            return true;
        }
    }
 
    return flag;
}
 
// Driver Program
int main()
{
    int K = 5;
    vector A = { 12, 1, 7, 7, 26, 18 };
    int N = 3;
 
    if (oddSum(A, N, K))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to return if
// odd sum is possible or not
static boolean oddSum(int []A,
                      int N, int K)
{
     
    // Stores distinct odd elements
    HashSet Odd = new HashSet();
     
    // Stores distinct even elements
    HashSet Even = new HashSet();
 
    // Iterating through given array
    for(int i = 0; i < N; i++)
    {
         
        // If element is even
        if (A[i] % 2 == 0)
        {
            Even.add(A[i]);
        }
             
        // If element is odd
        else
        {
            Odd.add(A[i]);
        }
    }
 
    // If atleast K elements
    // in the array are odd
    if (Odd.size() >= K)
        return true;
 
    boolean flag = false;
 
    // Check for all odd frequencies
    // of odd elements whether
    // sufficient even numbers
    // are present or not
    for(int i = 1; i < K; i += 2)
    {
         
        // Count of even numbers
        // required
        int needed = K - i;
             
        // If required even numbers
        // are present in the array
        if (needed <= Even.size())
        {
            return true;
        }
    }
    return flag;
}
 
// Driver code
public static void main(String[] args)
{
    int K = 5;
    int []A = { 12, 1, 7, 7, 26, 18 };
    int N = 3;
 
    if (oddSum(A, N, K))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for
# the above approach
 
# Function to return if
# odd sum is possible or not
def oddSum(A, N, K):
 
    # Stores distinct odd elements
    Odd = set([])
    # Stores distinct even elements
    Even = set([])
  
    # Iterating through given array
    for i in range (N):
  
        # If element is even
        if (A[i] % 2 == 0):
            Even.add(A[i])
        
        # If element is odd
        else:
            Odd.add(A[i])
  
    # If atleast K elements
    # in the array are odd
    if (len(Odd) >= K):
        return True
  
    flag = False
  
    # Check for all odd frequencies
    # of odd elements whether
    # sufficient even numbers
    # are present or not
    for i in range (1, K, 2):
  
        # Count of even numbers
        # required
        needed = K - i
  
        # If required even numbers
        # are present in the array
        if (needed <= len(Even)):
            return True
  
    return flag
  
# Driver Program
if __name__ == "__main__":
 
    K = 5
    A = [12, 1, 7,
         7, 26, 18]
    N = 3
  
    if (oddSum(A, N, K)):
        print ("YES")
    else:
        print("NO")
  
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to return if
// odd sum is possible or not
static bool oddSum(int []A, int N, int K)
{
     
    // Stores distinct odd elements
    HashSet Odd = new HashSet();
     
    // Stores distinct even elements
    HashSet Even = new HashSet();
 
    // Iterating through given array
    for(int i = 0; i < N; i++)
    {
         
        // If element is even
        if (A[i] % 2 == 0) 
        {
            Even.Add(A[i]);
        }
         
        // If element is odd
        else
        {
            Odd.Add(A[i]);
        }
    }
     
    // If atleast K elements
    // in the array are odd
    if (Odd.Count >= K)
        return true;
 
    bool flag = false;
 
    // Check for all odd frequencies
    // of odd elements whether
    // sufficient even numbers
    // are present or not
    for(int i = 1; i < K; i += 2)
    {
         
        // Count of even numbers
        // required
        int needed = K - i;
             
        // If required even numbers
        // are present in the array
        if (needed <= Even.Count)
        {
            return true;
        }
    }
    return flag;
}
 
// Driver code
public static void Main(String[] args)
{
    int K = 5;
    int []A = { 12, 1, 7, 7, 26, 18 };
    int N = 3;
 
    if (oddSum(A, N, K))
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
 
// This code is contributed by PrinciRaj1992


输出:
NO

时间复杂度: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。