Minimum number of increment-other operations to make all array elements equal.
Last Updated :
16 May, 2025
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[] = [1, 2, 3]
Output: 3
Explanation:
Operation 1 : Increase all except 3rd, we get 2, 3, 3
Operation 2 : Increase all except 3rd, we get 3, 4, 3
Operation 3 : Increase all except 2nd, we get 4, 4, 4
Input: arr[] = [4, 3, 4]
Output: 2
Explanation:
Operation 1 : Increase all except 3rd, we get 5, 4, 4
Operation 2 : Increase all except 1st, we get 5, 5, 5
[Naive Approach] Using Sorting – O(n^2logn) Time and O(1) Space
One important observation is, the order of operations does not matter, we always get the same count. For example, in the first case. If we reorder the operations, we end up incrementing every value by same amount.
The naive approach increments all elements except the maximum one in each step until all elements become equal..
The array is modified in-place, and no extra space is used other than the input array.
C++
#include <bits/stdc++.h>
using namespace std;
int minOps(vector<int>& arr) {
int n = arr.size();
int cnt = 0;
sort(arr.begin(),arr.end());
// break if all elements are equal
while (arr[0]!=arr[n-1]) {
//increment all the elements except the largest element;
for (int i = 0; i < n-1 ; i++) {
arr[i]++;
}
cnt++;
sort(arr.begin(),arr.end());
}
return cnt;
}
int main() {
vector<int> arr = {1,2,3};
cout<< minOps(arr) ;
}
Java
import java.util.*;
public class Main {
public static int minOps(int[] arr) {
int n = arr.length;
int cnt = 0;
Arrays.sort(arr);
// Loop until all elements are equal
while (arr[0] != arr[n - 1]) {
// Increment all elements except the largest element
for (int i = 0; i < n - 1; i++) {
arr[i]++;
}
cnt++;
Arrays.sort(arr); // Sort again after modifying the array
}
return cnt;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(minOps(arr));
}
}
Python
# Function to calculate minimum number of operations
def minOps(arr):
n = len(arr)
cnt = 0
arr.sort()
# Loop until all elements are equal
while arr[0] != arr[n - 1]:
# Increment all elements except
# the largest element
for i in range(n - 1):
arr[i] += 1
cnt += 1
# Sort again after modifying the array
arr.sort()
return cnt
arr = [1, 2, 3]
print(minOps(arr))
C#
using System;
using System.Linq;
class Program {
public static int MinOps(int[] arr) {
int n = arr.Length;
int cnt = 0;
Array.Sort(arr);
// Loop until all elements are equal
while (arr[0] != arr[n - 1]) {
// Increment all elements except the largest element
for (int i = 0; i < n - 1; i++) {
arr[i]++;
}
cnt++;
Array.Sort(arr); // Sort again after modifying the array
}
return cnt;
}
static void Main() {
int[] arr = {1, 2, 3};
Console.WriteLine(MinOps(arr));
}
}
JavaScript
// Function to calculate minimum number of operations
function minOps(arr) {
let n = arr.length;
let cnt = 0;
arr.sort((a, b) => a - b);
// Loop until all elements are equal
while (arr[0] !== arr[n - 1]) {
// Increment all elements except the largest element
for (let i = 0; i < n - 1; i++) {
arr[i]++;
}
cnt++;
arr.sort((a, b) => a - b); // Sort again after modifying the array
}
return cnt;
}
let arr = [1, 2, 3];
console.log(minOps(arr));
[Expected Approach] Using Direct Formula – O(n) Time and O(1) Space
If we took a closer look at each operation as well problem statement we will find that increasing all n-1 element except the largest one is similar to decreasing the largest element only. So, the smallest elements need not to decrease any more and rest of elements will got decremented upto the smallest one.
The optimized approach calculates the total sum of the array and identifies the smallest element.
It subtracts n * smallest element
from the sum to find the minimum number of operations.
C++
#include <bits/stdc++.h>
using namespace std;
int minOps(vector<int>& arr) {
int n = arr.size();
int sum = 0;
int minVal = *min_element(arr.begin(), arr.end());
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum - n * minVal;
}
int main() {
vector<int> arr = {4,3,4};
cout << minOps(arr) << endl;
return 0;
}
Java
import java.util.*;
public class Main {
public static int minOps(int[] arr) {
int n = arr.length;
int sum = 0;
int minVal = Arrays.stream(arr).min().getAsInt();
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum - n * minVal;
}
public static void main(String[] args) {
int[] arr = {4,3,4};
System.out.println(minOps(arr));
}
}
Python
def minOps(arr):
n = len(arr)
total_sum = sum(arr)
min_val = min(arr)
return total_sum - n * min_val
arr = [4,3,4]
print(minOps(arr))
C#
using System;
using System.Linq;
class Program {
public static int minOps(int[] arr) {
int n = arr.Length;
int sum = arr.Sum();
int minVal = arr.Min();
return sum - n * minVal;
}
static void Main() {
int[] arr = {4,3,4};
Console.WriteLine(minOps(arr));
}
}
JavaScript
function minOps(arr) {
let n = arr.length;
let sum = arr.reduce((a, b) => a + b, 0);
let minVal = Math.min(...arr);
return sum - n * minVal;
}
// Driver code
let arr = [4,3,4];
console.log(minOps(arr));
Similar Reads
Find the minimum number of operations required to make all array elements equal Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
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 operations required to make all elements in an array of first N odd numbers equal Given an array consisting of first N odd numbers, the task is to find the minimum number of operations required to make all the array elements equal by repeatedly selecting a pair and incrementing one element and decrementing the other element in the pair by 1. Examples: Input: N = 3Output: 2Explana
10 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
Make the array elements equal by performing given operations minimum number of times Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: a
7 min read
Minimum number of steps to make all elements of array equal Given two arrays A[] and B[] of the same length, the task is to find the minimum number of steps required to make all the elements of the array equal by replacing the element with the difference of the corresponding element from array B. Note: If this is not possible print -1.Examples: Input: A[] =
8 min read
Find the number of operations required to make all array elements Equal Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
6 min read
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:
11 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 operations to make all elements equal using the second array Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of: A[i] = A[i] - B[i], 0 <= i <= n-1 Note: If it's not possible to make array elements equal print -1.Examples: Input: A[] =
7 min read