Skip to content
Related Articles

Related Articles

Improve Article

Maximize remainder of sum of a pair of array elements with different parity modulo K

  • Last Updated : 25 Mar, 2021

Given an array arr[] of size N, consisting of N / 2 even and odd integers each, and an integer K, the task is to find the maximum remainder of sum of a pair of array elements of different parity modulo K.

Examples:

Input: arr[] = {3, 2, 4, 11, 6, 7}, K = 7
Output: 6
Explanation: 
Sum of a pair of array elements = 2 + 11
Sum % K = 13 % 7 = 6.
Therefore, the maximum remainder possible is 6.

Input: arr[] = {8, 11, 17, 16}, K = 13
Output: 12 

Approach: Follow the steps below to solve the problem:



  • Initialize a HashSet, say even, to store all even array elements.
  • Initialize a TreeSet, say odd, to store all odd array elements.
  • Initialize a variable, say max_rem, to store the maximum remainder possible.
  • Traverse the HashSet and for each element, find its complement and search for it in the set odd, which is less than equal to its complement.
  • Update max_rem with the sum of elements, and it’s complement.
  • Print the maximum remainder i.e. value of max_rem.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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<int> even;
 
    // Stores all odd numbers
    set<int> 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<Integer> even
          = new HashSet<>();
 
        // Stores all odd numbers
        TreeSet<Integer> 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
Output: 
6

 

Time Complexity: O(N * logN)
Auxiliary Space: O(N)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :