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

📅  最后修改于: 2021-10-26 06:48:05             🧑  作者: Mango

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

例子:

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

  • 初始化一个 HashSet,比如even ,以存储所有偶数数组元素。
  • 初始化一个 TreeSet,比如说奇数,以存储所有奇数数组元素。
  • 初始化一个变量,比如max_rem,以存储可能的最大余数。
  • 遍历 HashSet 并为每个元素找到它的补码并在集合奇数中搜索它,该集合小于等于其补码。
  • 用元素的总和更新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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程