📌  相关文章
📜  给定数组中具有偶数或奇数或总和为 K 的对数

📅  最后修改于: 2022-05-13 01:56:05.145000             🧑  作者: Mango

给定数组中具有偶数或奇数或总和为 K 的对数

给定一个大小不同的整数数组arr[] N和一个整数K ,任务是找到数组中可能的对总数,这样,要么都是偶数,要么都是奇数,或者这对的总和是K

注意:没有元素是多于一对的一部分。

例子:

方法:可以使用观察来解决该任务,如果最初将两个奇数组合在一起和两个偶数组合在一起,则可以观察到最大配对是可能的,然后考虑K的参与。
请按照以下步骤解决问题:

  • 让' odds '和' evens '分别存储数组中奇数偶数元素的计数
  • 如果“奇数”“偶数”都是奇数,则将奇数和偶数配对后,将留下 1 个奇数和 1 个偶数元素。要生成最大可能对,请查看是否存在奇偶校验不同总和等于 K,如果存在这样的对,则增加可能对的计数。
  • 否则,可能的配对总数将是奇数/2偶数/2总和

下面是上述方法的实现:

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to find the total possible pairs
void go(int ar[], int N, int K)
{
    // Stores the count of odd & even
    // elements in the array
    int odds = 0, evens = 0;
    for (int i = 0; i < N; i++) {
        if (ar[i] & 1)
            odds++;
        else
            evens++;
    }
 
    // Total pairs
    int ans = odds / 2 + evens / 2;
 
    // If number of both odds & even
    // elements are odd
    if ((odds & 1) && (evens & 1)) {
        unordered_map occ;
        for (int i = 0; i < N; i++)
            occ[ar[i]]++;
 
        // Check if there exists a pair
        // with different parity &
        // sum equals to K
        for (int i = 0; i < N; i++) {
            if (occ.find(K - ar[i])
                != occ.end()) {
                if (((ar[i] & 1) && !((K - ar[i]) & 1))
                    || (!(ar[i] & 1)
                        && ((K - ar[i])
                            & 1))) {
                    ans++;
                    break;
                }
            }
        }
    }
 
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    int N = 6, K = 7;
    int arr[N] = { 1, 2, 3, 4, 5, 6 };
    go(arr, N, K);
    return 0;
}


Java
// Java code to implement above approach
import java.util.HashMap;
class GFG {
 
  // Function to find the total possible pairs
  static void go(int[] ar, int N, int K) {
 
    // Stores the count of odd & even
    // elements in the array
    int odds = 0, evens = 0;
    for (int i = 0; i < N; i++) {
      if ((ar[i] & 1) > 0)
        odds++;
      else
        evens++;
    }
 
    // Total pairs
    int ans = odds / 2 + evens / 2;
 
    // If number of both odds & even
    // elements are odd
    if ((odds & 1) > 0 && (evens & 1) > 0) {
      HashMap occ = new HashMap();
      for (int i = 0; i < N; i++) {
        if (occ.containsKey(ar[i]))
          occ.put(ar[i], occ.get(ar[i]) + 1);
        else
          occ.put(ar[i], 1);
      }
 
      // Check if there exists a pair
      // with different parity &
      // sum equals to K
      for (int i = 0; i < N; i++) {
        if (occ.containsKey(K - ar[i])) {
          if (((ar[i] & 1) > 0
               && ((K - ar[i]) & 1) < 1)
              || ((ar[i] & 1) < 1
                  && ((K - ar[i]) & 1) > 0)) {
            ans++;
            break;
          }
        }
      }
    }
 
    System.out.println(ans);
  }
 
  // Driver Code
  public static void main(String args[]) {
    int N = 6, K = 7;
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    go(arr, N, K);
  }
}
 
// This code is contributed by gfgking


Python3
# Python code for the above approach
 
# Function to find the total possible pairs
def go(ar, N, K):
 
    # Stores the count of odd & even
    # elements in the array
    odds = 0
    evens = 0
    for i in range(N):
        if (ar[i] & 1):
            odds += 1
        else:
            evens += 1
 
    # Total pairs
    ans = odds // 2 + evens // 2;
 
    # If number of both odds & even
    # elements are odd
    if ((odds & 1) and (evens & 1)):
        occ = {};
        for i in range(N):
            if (arr[i] in occ):
                occ[ar[i]] += 1
            else:
                occ[arr[i]] = 1
         
 
        # Check if there exists a pair
        # with different parity &
        # sum equals to K
        for i in range(N):
            if ((K - ar[i]) in occ):
                if (((ar[i] & 1) and not ((K - ar[i]) & 1)) or (not (ar[i] & 1) and ((K - ar[i]) & 1))):
                    ans += 1
                    break;
 
    print(int(ans))
 
# Driver Code
N = 6
K = 7;
arr = [1, 2, 3, 4, 5, 6];
go(arr, N, K);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to implement above approach
using System;
using System.Collections.Generic;
class GFG {
 
    // Function to find the total possible pairs
    static void go(int[] ar, int N, int K)
    {
       
        // Stores the count of odd & even
        // elements in the array
        int odds = 0, evens = 0;
        for (int i = 0; i < N; i++) {
            if ((ar[i] & 1) > 0)
                odds++;
            else
                evens++;
        }
 
        // Total pairs
        int ans = odds / 2 + evens / 2;
 
        // If number of both odds & even
        // elements are odd
        if ((odds & 1) > 0 && (evens & 1) > 0) {
            Dictionary occ
                = new Dictionary();
            for (int i = 0; i < N; i++) {
                if (occ.ContainsKey(ar[i]))
                    occ[ar[i]]++;
                else
                    occ[ar[i]] = 1;
            }
            // Check if there exists a pair
            // with different parity &
            // sum equals to K
            for (int i = 0; i < N; i++) {
                if (occ.ContainsKey(K - ar[i])) {
                    if (((ar[i] & 1) > 0
                         && ((K - ar[i]) & 1) < 1)
                        || ((ar[i] & 1) < 1
                            && ((K - ar[i]) & 1) > 0)) {
                        ans++;
                        break;
                    }
                }
            }
        }
 
        Console.WriteLine(ans);
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 6, K = 7;
        int[] arr = { 1, 2, 3, 4, 5, 6 };
        go(arr, N, K);
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
3

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