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[] = [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, 5
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.
#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) ;
}
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));
}
}
# 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))
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));
}
}
// 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));
Output
3
[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 subtractsn * smallest element
from the sum to find the minimum number of operations.
#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;
}
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));
}
}
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))
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));
}
}
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));
Output
2