# Minimum increment operations to make K elements equal

Given an array **arr[]** of **N** elements and an integer **K**, the task is to make any **K** elements of the array equal by performing only increment operations i.e. in one operation, any element can be incremented by 1. Find the minimum number of operations required to make any **K** elements equal.

**Examples:**

Input:arr[] = {3, 1, 9, 10}, K = 3

Output:14

Increment 3 six times and 1 eight times for a total of

14 operations to make 3 elements equal to 9.

Input:arr[] = {5, 3, 10, 5}, K = 2

Output:0

No operations are required as first and last

elements are already equal.

**Naive approach:**

- Sort the array in increasing order.
- Now select
**K**elements and make them equal. - Choose the
**i**value as the largest value and make all elements just smaller than it equal to the^{th}**i**element.^{th} - Calculate the number of operations needed to make
**K**elements equal to the**i**element for all^{th}**i**. - The answer will be the minimum of all the possibilities.

Time Complexity of this approach will be **O(n*K + nlogn)**.

**Efficient approach:** the naive approach can be modified to calculate the minimum operations needed to make **K** elements equal to the **i ^{th}** element faster than

**O(K)**using the sliding window technique in constant time given that the operations required for making the

**1**

^{st}**K**elements equal to the

**K**element are known.

^{th}Let **C** be the operations needed or cost for making the elements in the range **[l, l + K – 1]** equal to the **(l + K – 1) ^{th}** element. Now to find the cost for the range

**[l + 1, l + K]**, the solution for the range

**[l, l + K – 1]**can be used.

Let

**C’**be the cost for the range

**[l + 1, l + K]**.

- Since we increment
**l**element to^{th}**(l + K – 1)**element,^{th}**C**includes the cost element(l + k – 1) – element(l) but**C’**does not need to include this cost.

So,**C’ = C – (element(l + k – 1) – element(l))** - Now
**C’**represents the cost of making all the elements in the range**[l + 1, l + K – 1]**equal to**(l + K – 1)**element.^{th}

Since, we need to make all elements equal to the**(l + K)**element instead of the^{th}**(l + K – 1)**element, we can increment these^{th}**k – 1**elements to the**(l + K)**element which makes^{th}**C’ = C’ + (k – 1) * (element(l + k) – element(l + k -1))**

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 ` `// increment operations required to make ` `// any k elements of the array equal ` `int` `minOperations(vector<` `int` `> ar, ` `int` `k) ` `{ ` ` ` `// Sort the array in increasing order ` ` ` `sort(ar.begin(), ar.end()); ` ` ` ` ` `// Calculate the number of operations ` ` ` `// needed to make 1st k elements equal to ` ` ` `// the kth element i.e. the 1st window ` ` ` `int` `opsNeeded = 0; ` ` ` `for` `(` `int` `i = 0; i < k; i++) { ` ` ` `opsNeeded += ar[k - 1] - ar[i]; ` ` ` `} ` ` ` ` ` `// Answer will be the minimum of all ` ` ` `// possible k sized windows ` ` ` `int` `ans = opsNeeded; ` ` ` ` ` `// Find the operations needed to make ` ` ` `// k elements equal to ith element ` ` ` `for` `(` `int` `i = k; i < ar.size(); i++) { ` ` ` ` ` `// Slide the window to the right and ` ` ` `// subtract increments spent on leftmost ` ` ` `// element of the previous window ` ` ` `opsNeeded = opsNeeded - (ar[i - 1] - ar[i - k]); ` ` ` ` ` `// Add increments needed to make the 1st k-1 ` ` ` `// elements of this window equal to the ` ` ` `// kth element of the current window ` ` ` `opsNeeded += (k - 1) * (ar[i] - ar[i - 1]); ` ` ` `ans = min(ans, opsNeeded); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = { 3, 1, 9, 100 }; ` ` ` `int` `n = arr.size(); ` ` ` `int` `k = 3; ` ` ` ` ` `cout << minOperations(arr, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum number of ` `# increment operations required to make ` `# any k elements of the array equal ` `def` `minOperations(ar, k): ` ` ` ` ` `# Sort the array in increasing order ` ` ` `arr ` `=` `sorted` `(ar) ` ` ` ` ` `# Calculate the number of operations ` ` ` `# needed to make 1st k elements equal to ` ` ` `# the kth element i.e. the 1st window ` ` ` `opsNeeded ` `=` `0` ` ` `for` `i ` `in` `range` `(k): ` ` ` `opsNeeded ` `+` `=` `ar[k ` `-` `1` `] ` `-` `ar[i] ` ` ` ` ` `# Answer will be the minimum of all ` ` ` `# possible k sized windows ` ` ` `ans ` `=` `opsNeeded ` ` ` ` ` `# Find the operations needed to make ` ` ` `# k elements equal to ith element ` ` ` `for` `i ` `in` `range` `(k, ` `len` `(ar)): ` ` ` ` ` `# Slide the window to the right and ` ` ` `# subtract increments spent on leftmost ` ` ` `# element of the previous window ` ` ` `opsNeeded ` `=` `opsNeeded ` `-` `(ar[i ` `-` `1` `] ` `-` `ar[i ` `-` `k]) ` ` ` ` ` `# Add increments needed to make the 1st k-1 ` ` ` `# elements of this window equal to the ` ` ` `# kth element of the current window ` ` ` `opsNeeded ` `+` `=` `(k ` `-` `1` `) ` `*` `(ar[i] ` `-` `ar[i ` `-` `1` `]) ` ` ` `ans ` `=` `min` `(ans, opsNeeded) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `arr ` `=` `[` `3` `, ` `1` `, ` `9` `, ` `100` `] ` `n ` `=` `len` `(arr) ` `k ` `=` `3` ` ` `print` `(minOperations(arr, k)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

14

**Time Complexity:** O(nlogn)

## Recommended Posts:

- Minimum increment by k operations to make all elements equal
- Minimum Increment / decrement to make array elements equal
- Minimum operations of given type to make all elements of a matrix equal
- Minimum increment or decrement operations required to make the array sorted
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Minimum operations to make two numbers equal
- Minimum operations to make frequency of all characters equal K
- Minimum operations required to make every element greater than or equal to K
- Minimum operations to make sum of neighbouring elements <= X
- Make all the array elements odd with minimum operations of given type
- Minimum number of operations on an array to make all elements 0
- Make array elements equal in Minimum Steps
- Minimum number of moves to make all elements equal
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum range increment operations to Sort an array

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.