Minimum increment operations to make K elements equal
Last Updated :
28 Feb, 2023
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, 100}, 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 ith value as the largest value and make all elements just smaller than it equal to the ith element.
- Calculate the number of operations needed to make K elements equal to the ith element for all i.
- The answer will be the minimum of all the possibilities.
C++14
#include <bits/stdc++.h>
using namespace std;
int minOperations(vector<int> ar, int& n, int& k)
{
// Sort the array in increasing order
sort(ar.begin(), ar.end());
int opsneeded, ans = INT_MAX;
for (int i = k; i < n; i++) {
opsneeded = 0;
for (int j = i - k; j < i; j++)
opsneeded += ar[i - 1] - ar[j];
ans = min(ans, opsneeded);
}
return ans;
}
int main()
{
vector<int> arr = { 3, 1, 9, 100 };
int n = arr.size();
int k = 3;
cout << minOperations(arr, n, k);
return 0;
}
// this code is contributed by prophet1999
Java
// JAVA code for the above approach
import java.util.*;
class GFG {
public static int minOperations(ArrayList<Integer> ar,
int n, int k)
{
// Sort the array in increasing order
Collections.sort(ar);
int opsneeded, ans = Integer.MAX_VALUE;
for (int i = k; i < n; i++) {
opsneeded = 0;
for (int j = i - k; j < i; j++)
opsneeded += ar.get(i - 1) - ar.get(j);
ans = Math.min(ans, opsneeded);
}
return ans;
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>(
Arrays.asList(3, 1, 9, 100));
int n = arr.size();
int k = 3;
System.out.print(minOperations(arr, n, k));
}
}
// This code is contributed by Taranpreet
Python3
import math
def minOperations(ar, n, k):
# Sort the array in increasing order
ar.sort()
opsneeded, ans = math.inf, math.inf
for i in range(k, n):
opsneeded = 0
for j in range(i - k, i):
opsneeded += ar[i - 1] - ar[j]
ans = min(ans, opsneeded)
return ans
# Driver code
arr = [3, 1, 9, 100]
n = len(arr)
k = 3
print(minOperations(arr, n, k))
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
public static int minOperations(List<int> ar, int n, int k) {
// Sort the array in increasing order
ar.Sort();
int opsneeded, ans = int.MaxValue;
for (int i = k; i < n; i++) {
opsneeded = 0;
for (int j = i - k; j < i; j++)
opsneeded += ar[i - 1] - ar[j];
ans = Math.Min(ans, opsneeded);
}
return ans;
}
// Driver code
public static void Main(string[] args) {
List<int> arr = new List<int>(new int[] {3, 1, 9, 100});
int n = arr.Count;
int k = 3;
Console.Write(minOperations(arr, n, k));
}
}
// This code is contributed by phasing17
JavaScript
// javascript approach for the same
function minOperations(ar,n,k){
// sort the array in increasing order
ar.sort((a,b) => a-b);
let opsneeded;
let ans=Infinity;
// loop to find the minimum operations
for (let i = k; i < n; i++) {
opsneeded = 0
for (let j = i - k; j < i; j++) {
opsneeded += ar[i - 1] - ar[j]
}
ans = Math.min(ans, opsneeded)
}
return ans;
}
// Driver code
let arr = [3, 1, 9, 100]
let k = 3
console.log(minOperations(arr,arr.length,k));
Time Complexity: Depends on sorting, it will be either O(n^2+n*K) or O(n log n+n*K)
Auxiliary Space: Depends on sorting, it will be either O(n) or O(1)
Efficient approach: the naive approach can be modified to calculate the minimum operations needed to make K elements equal to the ith element faster than O(K) using the sliding window technique in constant time given that the operations required for making the 1st K elements equal to the Kth element are known.
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 lth element to (l + K - 1)th element, 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)th element.
Since, we need to make all elements equal to the (l + K)th element instead of the (l + K - 1)th element, we can increment these k - 1 elements to the (l + K)th element which makes 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;
}
Java
// Java implementation of the approach
import java.util.Arrays;
class geeksforgeeks {
// Function to return the minimum number of
// increment operations required to make
// any k elements of the array equal
static int minOperations(int ar[], int k)
{
// Sort the array in increasing order
Arrays.sort(ar);
// 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.length; 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 = Math.min(ans, opsNeeded);
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 3, 1, 9, 100 };
int n = arr.length;
int k = 3;
System.out.printf("%d",minOperations(arr, k));
}
}
// This code is contributed by Atul_kumar_Shrivastava
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
ar = 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
C#
// C# implementation of the approach
using System;
class geeksforgeeks {
// Function to return the minimum number of
// increment operations required to make
// any k elements of the array equal
static int minOperations(int[] ar, int k)
{
// Sort the array in increasing order
Array.Sort(ar);
// 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.Length; 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 = Math.Min(ans, opsNeeded);
}
return ans;
}
// Driver code
public static void Main()
{
int[] arr = { 3, 1, 9, 100 };
int n = arr.Length;
int k = 3;
Console.Write(minOperations(arr, k));
}
}
// This code is contributed by AbhiThakur
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the minimum number of
// increment operations required to make
// any k elements of the array equal
function minOperations(ar,k)
{
// Sort the array in increasing order
ar.sort(function(a,b){return a-b});
// Calculate the number of operations
// needed to make 1st k elements equal to
// the kth element i.e. the 1st window
let opsNeeded = 0;
for (let i = 0; i < k; i++) {
opsNeeded += ar[k - 1] - ar[i];
}
// Answer will be the minimum of all
// possible k sized windows
let ans = opsNeeded;
// Find the operations needed to make
// k elements equal to ith element
for (let i = k; i < ar.length; 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 = Math.min(ans, opsNeeded);
}
return ans;
}
// Driver code
let arr=[3, 1, 9, 100 ];
let n = arr.length;
let k = 3;
document.write(minOperations(arr, k));
// This code is contributed by patel2127
</script>
Time Complexity: Depends on sorting, it will be either O(n^2) or O(n log n)
Auxiliary Space: Depends on sorting, it will be either O(n) or O(1)
Similar Reads
Minimum increment by k operations to make all equal
You are given an array of n-elements, you have to find the number of operations needed to make all elements of array equal. Where a single operation can increment an element by k. If it is not possible to make all elements equal print -1.Example : Input : arr[] = {4, 7, 19, 16}, k = 3Output : 10Inpu
6 min read
Minimum operation to make all elements equal in array
Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Minimum number of increment-other operations to make all array elements equal.
We are given an array consisting of n elements. At each operation we can select any one element and increase rest of n-1 elements by 1. We have to make all elements equal performing such operation as many times you wish. Find the minimum number of operations needed for this.Examples: Input: arr[] =
5 min read
Minimum operations required to make two elements equal in Array
Given array A[] of size N and integer X, the task is to find the minimum number of operations to make any two elements equal in the array. In one operation choose any element A[i] and replace it with A[i] & X. where & is bitwise AND. If such operations do not exist print -1. Examples: Input:
9 min read
Minimum operations to make frequency of all characters equal K
Given a string S of length N. The task is to find the minimum number of steps required on strings, so that it has exactly K different alphabets all with the same frequency.Note: In one step, we can change a letter to any other letter.Examples: Input: S = "abbc", N = 4, K = 2 Output: 1 In one step co
8 min read
Minimum Cost to make all array elements equal using given operations
Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t
12 min read
Minimum Increment / decrement to make array elements equal
Given an array of integers where 1 \leq A[i] \leq 10^{18} . In one operation you can either Increment/Decrement any element by 1. The task is to find the minimum operations needed to be performed on the array elements to make all array elements equal. Examples: Input : A[] = { 1, 5, 7, 10 } Output :
7 min read
Minimum operations required to make every element greater than or equal to K
Given an array of length N. The task is to convert it into a sequence in which all elements are greater than or equal to K.The only operation allowed is taking two smallest elements of the sequence and replace them by their LCM. Find the minimum number of operations required. If it is impossible to
7 min read
Minimum operations required to make all the array elements equal
Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read
Minimum number of operations required to make all elements equal
Given an array arr[] of length N along with an integer M. All the elements of arr[] are in the range [1, N]. Then your task is to output the minimum number of operations required to make all elements equal given that in one operation you can select at most M elements and increment all of them by 1.
5 min read