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++
#include <bits/stdc++.h>
using namespace std;
void maxRemainder( int A[], int N, int K)
{
unordered_set< int > even;
set< int > odd;
for ( int i = 0; i < N; i++)
{
int num = A[i];
if (num % 2 == 0)
even.insert(num % K);
else
odd.insert(num % K);
}
int max_rem = 0;
for ( int x : even)
{
int y = K - 1 - x;
auto it = odd.upper_bound(y);
if (it != odd.begin())
{
it--;
max_rem = max(max_rem, x + *it);
}
}
cout << max_rem;
}
int main()
{
int arr[] = { 3, 2, 4, 11, 6, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 7;
maxRemainder(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void maxRemainder( int A[],
int N, int K)
{
HashSet<Integer> even
= new HashSet<>();
TreeSet<Integer> odd
= new TreeSet<>();
for ( int num : A) {
if (num % 2 == 0 )
even.add(num % K);
else
odd.add(num % K);
}
int max_rem = 0 ;
for ( int x : even) {
int y = K - 1 - x;
if (odd.floor(y) != null )
max_rem
= Math.max(
max_rem,
x + odd.floor(y));
}
System.out.print(max_rem);
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 4 , 11 , 6 , 7 };
int N = arr.length;
int K = 7 ;
maxRemainder(arr, N, K);
}
}
|
Python3
from bisect import bisect_left
def maxRemainder(A, N, K):
even = {}
odd = {}
for i in range (N):
num = A[i]
if (num % 2 = = 0 ):
even[num % K] = 1
else :
odd[num % K] = 1
max_rem = 0
for x in even:
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 (max_rem)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 4 , 11 , 6 , 7 ]
N = len (arr)
K = 7
maxRemainder(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void MaxRemainder( int [] A, int N, int K)
{
HashSet< int > even = new HashSet< int >();
SortedSet< int > odd = new SortedSet< int >();
foreach ( int num in A)
{
if (num % 2 == 0)
even.Add(num % K);
else
odd.Add(num % K);
}
int max_rem = 0;
foreach ( int x in even)
{
int y = K - 1 - x;
if (odd.Min <= y)
max_rem = Math.Max(max_rem, x + ( int )odd.GetViewBetween( int .MinValue, y).Max);
}
Console.Write(max_rem);
}
public static void Main( string [] args)
{
int [] arr = { 3, 2, 4, 11, 6, 7 };
int N = arr.Length;
int K = 7;
MaxRemainder(arr, N, K);
}
}
|
Javascript
function upper_bound(arr, X)
{
let mid;
let low = 0;
let high = arr.length;
while (low < high) {
mid = low + Math.floor((high - low) / 2);
if (X >= arr[mid]) {
low = mid + 1;
}
else {
high = mid;
}
}
if (low < N && arr[low] <= X) {
low++;
}
return low;
}
function maxRemainder(A, N, K)
{
let even = new Array();
let odd = new Array();
for (let i = 0; i < N; i++)
{
let num = A[i];
if (num % 2 == 0 && !even.includes(num%K)){
even.push(num % K);
}
else if (num%2 != 0 && !odd.includes(num%K)){
odd.push(num % K);
}
}
odd.sort();
let max_rem = 0;
for (let i = 0;i < even.length; i++){
let x = even[i];
let y = K - 1 - x;
let it = upper_bound(odd, y);
if (it != 0)
{
it--;
max_rem = Math.max(max_rem, x + odd[it]);
}
}
console.log(max_rem);
}
let arr = [3, 2, 4, 11, 6, 7];
let N = arr.length;
let K = 7;
maxRemainder(arr, N, K);
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Jan, 2023
Like Article
Save Article