Given an array **arr[]** of **N** integers and an integer **M** where **N % M = 0**. The task is to find the minimum number of operations that need to be performed on the array to make **c _{0} = c_{1} = ….. = c_{M – 1} = N / M** where

**c**is the

_{r}**number of elements**in the given array

**having remainder r**when divided by

**M**. In each operation, any array element can be incremented by

**1**.

**Examples:**

Input:arr[] = {1, 2, 3}, M = 3

Output:0

After performing the modulus operation on the given array, the array becomes {0, 1, 2}

And count of c_{0}= c_{1}= c_{2}= n / m = 1.

So, no any additional operations are required.

Input:arr[] = {3, 2, 0, 6, 10, 12}, M = 3

Output:3

After performing the modulus operation on the given array, the array becomes {0, 2, 0, 0, 1, 0}

And count of c_{0}= 4, c_{1}= 1 and c_{2}= 1. To make c_{0}= c_{1}= c_{2}= n / m = 2.

Add 1 to 6 and 2 to 12 then the array becomes {3, 2, 0, 7, 10, 14} and c_{0}= c_{1}= c_{2}= n / m = 2.

**Approach:** For each **i** from **0** to **m – 1**, find all the elements of the array that are congruent to **i modulo m** and store their indices in a list. Also, create a vector called extra, and let **k = n / m**.

We have to cycle from **0 to m – 1** twice. For each **i** from **0 to m – 1**, if there are more elements than **k** in the list, remove the extra elements from this list and add them to extra. If instead there are lesser elements than **k** then remove the last few elements from the vector extra. For every removed index **idx**, increase **arr[idx]** by **(i – arr[idx]) % m**.

It is obvious that after the first **m** iterations, every list will have size at most **k** and after **m** more iterations all lists will have the same sizes i.e. **k**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum ` `// number of operations required ` `int` `minOperations(` `int` `n, ` `int` `a[], ` `int` `m) ` `{ ` ` ` `int` `k = n / m; ` ` ` ` ` `// To store modulos values ` ` ` `vector<vector<` `int` `> > val(m); ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `val[a[i] % m].push_back(i); ` ` ` `} ` ` ` ` ` `long` `long` `ans = 0; ` ` ` `vector<pair<` `int` `, ` `int` `> > extra; ` ` ` ` ` `for` `(` `int` `i = 0; i < 2 * m; ++i) { ` ` ` `int` `cur = i % m; ` ` ` ` ` `// If it's size greater than k ` ` ` `// it needed to be decreased ` ` ` `while` `(` `int` `(val[cur].size()) > k) { ` ` ` `int` `elem = val[cur].back(); ` ` ` `val[cur].pop_back(); ` ` ` `extra.push_back(make_pair(elem, i)); ` ` ` `} ` ` ` ` ` `// If it's size is less than k ` ` ` `// it needed to be increased ` ` ` `while` `(` `int` `(val[cur].size()) < k && !extra.empty()) { ` ` ` `int` `elem = extra.back().first; ` ` ` `int` `mmod = extra.back().second; ` ` ` `extra.pop_back(); ` ` ` `val[cur].push_back(elem); ` ` ` `ans += i - mmod; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `m = 3; ` ` ` ` ` `int` `a[] = { 3, 2, 0, 6, 10, 12 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << minOperations(n, a, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum ` `# number of operations required ` `def` `minOperations(n, a, m): ` ` ` ` ` `k ` `=` `n ` `/` `/` `m ` ` ` ` ` `# To store modulos values ` ` ` `val ` `=` `[[] ` `for` `i ` `in` `range` `(m)] ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `val[a[i] ` `%` `m].append(i) ` ` ` ` ` `ans ` `=` `0` ` ` `extra ` `=` `[] ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `2` `*` `m): ` ` ` `cur ` `=` `i ` `%` `m ` ` ` ` ` `# If it's size greater than k ` ` ` `# it needed to be decreased ` ` ` `while` `len` `(val[cur]) > k: ` ` ` `elem ` `=` `val[cur].pop() ` ` ` `extra.append((elem, i)) ` ` ` ` ` `# If it's size is less than k ` ` ` `# it needed to be increased ` ` ` `while` `(` `len` `(val[cur]) < k ` `and` ` ` `len` `(extra) > ` `0` `): ` ` ` `elem ` `=` `extra[` `-` `1` `][` `0` `] ` ` ` `mmod ` `=` `extra[` `-` `1` `][` `1` `] ` ` ` `extra.pop() ` ` ` `val[cur].append(elem) ` ` ` `ans ` `+` `=` `i ` `-` `mmod ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `m ` `=` `3` ` ` ` ` `a ` `=` `[` `3` `, ` `2` `, ` `0` `, ` `6` `, ` `10` `, ` `12` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `print` `(minOperations(n, a, m)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Check if N leaves only distinct remainders on division by all values up to K
- Queries for counts of multiples in an array
- Find minimum number of merge operations to make an array palindrome
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K
- Minimum increment operations to make the array in increasing order
- Find minimum operations needed to make an Array beautiful
- Minimum Increment operations to make Array unique
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Make all the array elements odd with minimum operations of given type
- Minimum increment or decrement operations required to make the array sorted
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations
- Minimum pair merge operations required to make Array non-increasing

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.