📌  相关文章
📜  最大化一对具有不同奇偶校验模K的数组元素之和的余数

📅  最后修改于: 2021-05-17 22:06:11             🧑  作者: Mango

给定大小为N的数组arr [] ,该数组arr []分别由N / 2个偶数和奇数整数以及一个整数K组成,任务是找到一对不同的奇偶校验模K的数组元素之和的最大余数。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个HashSet,例如even ,以存储所有偶数数组元素。
  • 初始化一个TreeSet(例如,奇数)以存储所有奇数数组元素。
  • 初始化一个变量,例如max_rem,以存储可能的最大余数。
  • 遍历HashSet并为每个元素找到其补码,然后在小于等于其补码的set奇数中搜索它。
  • 用元素的总和更新max_rem ,它是补码。
  • 打印最大余数,即max_rem的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum
// remainder of sum of a pair
// of array elements modulo K
void maxRemainder(int A[], int N, int K)
{
     
    // Stores all even numbers
    unordered_set even;
 
    // Stores all odd numbers
    set odd;
 
    // Segregate remainders of even
    // and odd numbers in respective sets
    for(int i = 0; i < N; i++)
    {
        int num = A[i];
         
        if (num % 2 == 0)
            even.insert(num % K);
        else
            odd.insert(num % K);
    }
 
    // Stores the maximum
    // remainder obtained
    int max_rem = 0;
 
    // Find the complement of remainder
    // of each even number in odd set
    for(int x : even)
    {
         
        // Find the complement
        // of remiander x
        int y = K - 1 - x;
 
        auto it = odd.upper_bound(y);
        if (it != odd.begin())
        {
            it--;
            max_rem = max(max_rem, x + *it);
        }
    }
 
    // Print the answer
    cout << max_rem;
}
 
// Driver code
int main()
{
     
    // Given array
    int arr[] = { 3, 2, 4, 11, 6, 7 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of K
    int K = 7;
 
    maxRemainder(arr, N, K);
 
    return 0;
}
 
// This code is contributed by Kingash


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find the maximum
    // remainder of sum of a pair
    // of array elements modulo K
    static void maxRemainder(int A[],
                             int N, int K)
    {
        // Stores all even numbers
        HashSet even
          = new HashSet<>();
 
        // Stores all odd numbers
        TreeSet odd
          = new TreeSet<>();
 
        // Segregate remainders of even
        // and odd numbers in respective sets
        for (int num : A) {
            if (num % 2 == 0)
                even.add(num % K);
            else
                odd.add(num % K);
        }
 
        // Stores the maximum
        // remainder obtained
        int max_rem = 0;
 
        // Find the complement of remainder
        // of each even number in odd set
        for (int x : even) {
 
            // Find the complement
            // of remiander x
            int y = K - 1 - x;
            if (odd.floor(y) != null)
                max_rem
                    = Math.max(
              max_rem,
              x + odd.floor(y));
        }
 
        // Print the answer
        System.out.print(max_rem);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array
        int arr[] = { 3, 2, 4, 11, 6, 7 };
 
        // Size of the array
        int N = arr.length;
 
        // Given value of K
        int K = 7;
 
        maxRemainder(arr, N, K);
    }
}


Python3
# Python3 program for the above approach
from bisect import bisect_left
 
# Function to find the maximum
# remainder of sum of a pair
# of array elements modulo K
def maxRemainder(A, N, K):
     
    # Stores all even numbers
    even = {}
 
    # Stores all odd numbers
    odd = {}
 
    # Segregate remainders of even
    # and odd numbers in respective sets
    for i in range(N):
        num = A[i]
 
        if (num % 2 == 0):
            even[num % K] = 1
        else:
            odd[num % K] = 1
 
    # Stores the maximum
    # remainder obtained
    max_rem = 0
 
    # Find the complement of remainder
    # of each even number in odd set
    for x in even:
         
        # Find the complement
        # of remiander x
        y = K - 1 - x
        od = list(odd.keys())
        it = bisect_left(od, y)
         
        if (it != 0):
            max_rem = max(max_rem, x + od[it])
             
    # Print the answer
    print (max_rem)
 
# Driver code
if __name__ == '__main__':
     
    # Given array
    arr = [3, 2, 4, 11, 6, 7]
 
    # Size of the array
    N = len(arr)
 
    # Given value of K
    K = 7
 
    maxRemainder(arr, N, K)
     
# This code is contributed by mohit kumar 29


输出:
6

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