
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Count pairs in array whose sum is divisible by K in C++
- Count the number of elements in an array which are divisible by k in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to check if array pairs are divisible by k or not using Python
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++
- Program to find max number of K-sum pairs in Python
- Maximize Sum Of Array After K Negations in Python
- Check whether sum of digits at odd places of a number is divisible by K in Python
- Count pairs in array whose sum is divisible by 4 in C++
- Count all sub-arrays having sum divisible by k
- Maximize array sum after K negation in C++
- Largest K digit number divisible by X in C++
