# Maximum sum of pair values such that value of pairs of same group should be a multiple of i in range [1, N]

• Last Updated : 17 Nov, 2021

Given an array of pairs arr[] of size N, where the first element of the pair is the value of the pair and the second element is the group at which this pair belongs, the task is to find the maximum sum of pair values, such that the number of pairs of the same group should be a multiple of i for all i in the range [1, N].

Examples:

Input: arr[] = {{5, 3}, {9, 3}, {6, 3}, {7, 3}, {9, 3}, {7, 3}}, N = 6
Output : {43, 43, 43, 32, 38, 43}
Explanation:
There are 6 pairs of groups 3.
For i = 1, select all the pairs of group 3, since 6 % 1 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.
For i = 2, select all the pairs of group 3 since 6 % 2 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.
For i = 3, select all the pairs of group 3 since 6 % 3 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.
For i = 4, select 4 pairs of group 3 whose sum of value is largest, then the sum will be 9 + 9 + 7 + 7 = 32.
For i = 5, select 5 pairs of group 3 whose sum of value is largest, then the sum will be 9 + 9 + 7 + 7 + 6 = 38.
For i = 6, select all the pairs of group 3 since 6 % 6 = 0, then the sum will be 5 + 9 + 6 + 7 + 9 + 7 = 43.

Input: arr[] = {{6, 1}, {8, 2}, {3, 1}, {1, 2}, {5, 1}, {1, 2}, {5, 1}}, N = 7
Output : {29, 28, 26, 19, 0, 0, 0}

Approach: The simplest idea to solve the problem is to segregate the elements of the array on the basis of the group and then use the prefix sum array to find the sum of elements from each group in O(1). Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum sum of``// pair values, such that the number of``// pairs of the same group should be a``// multiple of i for all i in the``// range [1, N]``void` `findMaximumSumValue(vector > arr, ``int` `n)``{` `    ``// Map for storing elements of same group``    ``// together``    ``map<``int``, vector<``int``> > mp;` `    ``// Segregate students on the basis of group``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``mp[arr[i].second - 1].push_back(arr[i].first);``    ``}` `    ``vector > v;` `    ``// Pushing all the vectors in the map to v``    ``for` `(``auto` `i : mp) {``        ``v.push_back(i.second);` `        ``// Sort all the groups in ascending order``        ``sort(v.back().begin(), v.back().end());``    ``}` `    ``// Vector to store answer``    ``vector<``int``> ans(n, 0);` `    ``// Vector to store prefix sum array``    ``// for each group``    ``vector > it;` `    ``// Traverse through the groups``    ``for` `(``auto` `i : v) {``        ``vector<``int``> c;` `        ``// Save prefix sum array in vector C``        ``for` `(``int` `j = 0; j < (``int``)i.size(); j++) {``            ``if` `(j == 0) {``                ``c.push_back(i[j]);``            ``}``            ``else` `{``                ``c.push_back(c.back() + i[j]);``            ``}``        ``}` `        ``// Insert the prefix sum c in it``        ``it.push_back(c);``    ``}` `    ``// Traverse through the prefix function of each group``    ``for` `(``auto` `i : it) {` `        ``// Traverse for all number of elements in i``        ``for` `(``int` `j = 1; j <= (``int``)i.size(); j++) {` `            ``// Check the number students to be left.``            ``int` `left = (``int``)i.size() % j;``            ``int` `del = 0;` `            ``// If left is greater than 0 subtract the``            ``// value of left smallest elements``            ``if` `(left > 0)``                ``del = i[left - 1];``            ``ans[j - 1] += (i.back() - del);``        ``}``    ``}` `    ``// Print the answer list for every``    ``// possible size``    ``for` `(``int` `i = 0; i < n; i++) {``        ``cout << ans[i] << ``" "``;``    ``}` `    ``cout << endl;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``vector > arr``        ``= { { 5, 3 }, { 9, 3 }, { 6, 3 },``            ``{ 7, 3 }, { 9, 3 }, { 7, 3 } };` `    ``int` `N = arr.size();` `    ``// Function Call``    ``findMaximumSumValue(arr, N);` `    ``return` `0;``}`

## Python3

 `# Python 3 program for the above approach` `# Function to find the maximum sum of``# pair values, such that the number of``# pairs of the same group should be a``# multiple of i for all i in the``# range [1, N]``def` `findMaximumSumValue(arr, n):` `    ``# Map for storing elements of same group``    ``# together``    ``mp ``=` `{}` `    ``# Segregate students on the basis of group``    ``for` `i ``in` `range``(n):``        ``if` `(arr[i][``1``]``-``1``) ``not` `in` `mp:``            ``mp[arr[i][``1``] ``-` `1``] ``=` `[]` `        ``mp[arr[i][``1``] ``-` `1``].append(arr[i][``0``])` `    ``v ``=` `[]` `    ``# Pushing all the vectors in the map to v``    ``for` `i ``in` `mp:``        ``v.append(mp[i])` `        ``# Sort all the groups in ascending order``        ``v[``0``].sort()` `    ``# Vector to store answer``    ``ans ``=` `[``0``]``*``(n)` `    ``# Vector to store prefix sum array``    ``# for each group``    ``it ``=` `[]` `    ``# Traverse through the groups``    ``for` `i ``in` `v:``        ``c ``=` `[]` `        ``# Save prefix sum array in vector C``        ``for` `j ``in` `range``(``len``(i)):``            ``if` `(j ``=``=` `0``):``                ``c.append(i[j])` `            ``else``:``                ``c.append(c[``-``1``] ``+` `i[j])` `        ``# Insert the prefix sum c in it``        ``it.append(c)` `    ``# Traverse through the prefix function of each group``    ``for` `i ``in` `it:` `        ``# Traverse for all number of elements in i``        ``for` `j ``in` `range``(``1``, ``len``(i) ``+` `1``):` `            ``# Check the number students to be left.``            ``left ``=` `len``(i) ``%` `j``            ``dele ``=` `0` `            ``# If left is greater than 0 subtract the``            ``# value of left smallest elements``            ``if` `(left > ``0``):``                ``dele ``=` `i[left ``-` `1``]``            ``ans[j ``-` `1``] ``+``=` `(i[``-``1``] ``-` `dele)` `    ``# Print the answer list for every``    ``# possible size``    ``for` `i ``in` `range``(n):``        ``print``(ans[i], end``=``" "``)` `    ``print``()` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Given Input``    ``arr ``=` `[[``5``, ``3``], [``9``, ``3``], [``6``, ``3``],``           ``[``7``, ``3``], [``9``, ``3``], [``7``, ``3``]]` `    ``N ``=` `len``(arr)` `    ``# Function Call``    ``findMaximumSumValue(arr, N)` `    ``# This code is contributed by ukasp.`

Output:

`43 43 43 32 38 43`

Time Complexity: O(N2)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up