Related Articles

# Minimum number of increment-other operations to make all array elements equal.

• Difficulty Level : Medium
• Last Updated : 21 Apr, 2021

We are given an array consisting of n elements. At each operation you can select any one element and increase rest of n-1 elements by 1. You 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 : Minimum Operation = 3
Explanation :
operation | increased elements | after increment
1     |    1, 2            | 2, 3, 3
2     |    1, 2            | 3, 4, 3
3     |    1, 3            | 4, 4, 4

Input : arr[] = {4, 3, 4}
Output : Minimum Operation = 2
Explanation :
operation | increased elements | after increment
1    |    1, 2           | 5, 4, 4
2    |    2, 3           | 5, 5, 5```

Brute force : A simple way to make all elements equal is that at each step find the largest elements and then increase all rest n-1 elements by 1. We should keep doing this operation till all elements become equal. Time Complexity : O(n^2)
Better Approach : 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 smallest one. In this way the total number of operation required for making all elements equal will be arraySum – n * (smallesteElement). Time complexity will be same as that of finding smallest elements and array sum i.e. O(n).

```// find array sum
sum = arraySum (int arr[], int n);

// find the smallest element from array
small = smallest (int arr, int n);

// calculate min operation required
minOperation = sum - (n * small);

// return result
return minOperation;```

## C++

 `// CPP for finding minimum operation required``#include``using` `namespace` `std;` `// function for finding array sum``int` `arraySum (``int` `arr[], ``int` `n)``{``    ``int` `sum = 0;``    ``for` `(``int` `i=0; i

## Java

 `// Java program to find min operation``import` `java.io.*;`` ` `class` `minOeration``{``    ``/* Function to print minimum operation required``       ``to make all elements of an array equal */``    ``static` `void` `printMinOp(``int` `arr[])``    ``{``        ``int` `arraySum, smallest, arr_size = arr.length;``        ``arraySum = ``0``;``        ``smallest = arr[``0``];``        ``for` `(``int` `i = ``0``; i < arr_size ; i ++)``        ``{``            ``/* If current element is smaller than``               ``update smallest */``            ``if` `(arr[i] < smallest)           ``                ``smallest = arr[i];           `` ` `            ``/*find array sum */``            ``arraySum += arr[i];``        ``}` `        ``int` `minOperation = arraySum - arr_size * smallest;` `        ``/* Print min operation required */` `        ``System.out.println(``"Minimum Operation = "` `+``                               ``minOperation);` `    ``}`` ` `    ``/* Driver program to test above functions */``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `arr[] = {``5``, ``6``, ``2``, ``4``, ``3``};``        ``printMinOp(arr);``    ``}``}`

## Python3

 `# Python 3 for finding minimum``# operation required` `# function for finding array sum``def` `arraySum (arr, n) :` `    ``sm ``=` `0``    ``i ``=` `0` `    ``while``(i

## C#

 `// C# program to find min operation``using` `System;` `class` `GFG {``    ` `    ``/* Function to print minimum``    ``operation required to make all``    ``elements of an array equal */``    ``static` `void` `printMinOp(``int` `[]arr)``    ``{``        ``int` `arraySum, smallest,``        ``arr_size = arr.Length;``        ``arraySum = 0;``        ``smallest = arr[0];``        ` `        ``for` `(``int` `i = 0; i < arr_size ; i ++)``        ``{``            ``/* If current element is``            ``smaller than update smallest */``            ``if` `(arr[i] < smallest)        ``                ``smallest = arr[i];        ` `            ``/*find array sum */``            ``arraySum += arr[i];``        ``}` `        ``int` `minOperation = arraySum - ``                        ``arr_size * smallest;` `        ``/* Print min operation required */``        ``Console.Write(``"Minimum Operation = "` `+``                            ``minOperation);``    ``}` `    ``/* Driver program to test above``    ``functions */``    ``public` `static` `void` `Main ()``    ``{``        ``int` `[]arr = {5, 6, 2, 4, 3};``        ` `        ``printMinOp(arr);``    ``}``}` `// This code is contributed by nitin mittal.`

## PHP

 ``

## Javascript

 ``

Output:

`Minimum Operation = 10`

This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up