Maximize the number of sum pairs which are divisible by K in C++


Given the task is to calculate the maximum number of pairs arr[i] + arr[j] that are divisible by K where arr[] is an array containing N integers.

Given the condition that a particular index number cannot be used in more than one pair.

Input

arr[]={1, 2 ,5 ,8 ,3 }, K=2

Output 

2

Explanation − The desired pairs are: (0,2), (1,3) as 1+5=6 and 2+8=10 . Both 6 and 10 are divisible by 2.

Alternative answers could be the pairs: (0,4), (1,3) or (2,4), (1,3) but the answer remains the same, that is, 2.

Input 

arr[]={1 ,3 ,5 ,2 ,3 ,4 }, K=3

Output 

3

Approach used in the below program as follows

  • In the variable n of type int store the size of the array

  • In the function MaxPairs() use unordered map and increase the value as um[arr[i]%K] by one for each element of the array.

  • Iterate and get every possible um value.

  • If the um value is zero then the number of pairs will become um[0]/2.

  • Then pairs can be formed from every um value ‘a’ by using the minimum of (um[a], um[Ka])

  • Subtract from the um value, the number of pairs used.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int MaxPairs(int arr[], int size, int K){
   unordered_map<int, int> um;
   for (int i = 0; i < size; i++){
      um[arr[i] % K]++;
   }
   int count = 0;
   /*Iteration for all the number that are less than the hash value*/
   for (auto it : um){
      // If the number is 0
      if (it.first == 0){
         //Taking half since same number
         count += it.second / 2;
         if (it.first % 2 == 0){
            um[it.first] = 0;
         }
         else{
            um[it.first] = 1;
         }
      }
      else{
         int first = it.first;
         int second = K - it.first;
         // Check for minimal occurrence
         if (um[first] < um[second]){
            //Taking the minimal
            count += um[first];
            //Subtracting the used pairs
            um[second] -= um[first];
            um[first] = 0;
         }
         else if (um[first] > um[second]){
            // Taking the minimal
            count += um[second];
            //Subtracting the used pairs
            um[first] -= um[second];
            um[second] = 0;
         }
         else{
            //Checking if the numbers are same
            if (first == second){
               //If same then number of pairs will be half
               count += it.second / 2;
               //Checking for remaining
               if (it.first % 2 == 0)
                  um[it.first] = 0;
               else
                  um[it.first] = 1;
            }
            else{
               //Storing the number of pairs
               count += um[first];
               um[first] = 0;
               um[second] = 0;
            }
         }
      }
   }
   return count;
}
//Main function
int main(){
   int arr[] = { 3, 6, 7, 9, 4, 4, 10 };
   int size = sizeof(arr) / sizeof(arr[0]);
   int K = 2;
   cout<<"Maximize the number of sum pairs which are divisible by K is: "<<MaxPairs(arr, size, K);
   return 0;
}

Output

If we run the above code, we will get the following output −

Maximize the number of sum pairs which are divisible by K is: 3

Updated on: 14-Aug-2020

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements