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 * (smallestElement). 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;
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int arraySum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int smallest( int arr[], int n)
{
int small = INT_MAX;
for ( int i = 0; i < n; i++)
if (arr[i] < small)
small = arr[i];
return small;
}
int minOp( int arr[], int n)
{
int sum = arraySum(arr, n);
int small = smallest(arr, n);
int minOperation = sum - (n * small);
return minOperation;
}
int main()
{
int arr[] = { 5, 6, 2, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Minimum Operation = " << minOp(arr, n);
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
int minOp ( int arr[], int n)
{
int sum = accumulate(arr,arr+n,0);
int small = *min_element(arr,arr+n);
int minOperation = sum - (n * small);
return minOperation;
}
int main()
{
int arr[] = {5, 6, 2, 4, 3};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Minimum Operation = " << minOp (arr, n);
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
int arraySum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int smallest( int arr[], int n)
{
int small = INT_MAX;
for ( int i = 0; i < n; i++)
if (arr[i] < small)
small = arr[i];
return small;
}
int minOp( int arr[], int n)
{
int sum = arraySum(arr, n);
int small = smallest(arr, n);
int minOperation = sum - (n * small);
return minOperation;
}
int main()
{
int arr[] = { 5, 6, 2, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Minimum Operation = %d" , minOp(arr, n));
return 0;
}
|
Java
import java.io.*;
class minOperation {
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 (arr[i] < smallest)
smallest = arr[i];
arraySum += arr[i];
}
int minOperation = arraySum - arr_size * smallest;
System.out.println( "Minimum Operation = " + minOperation);
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 2 , 4 , 3 };
printMinOp(arr);
}
}
|
Python3
def minOp (arr, n) :
sm = sum (arr)
small = min (arr)
minOperation = sm - (n * small)
return minOperation
arr = [ 5 , 6 , 2 , 4 , 3 ]
n = len (arr)
print ( "Minimum Operation = " , minOp (arr, n))
|
C#
using System;
class GFG {
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 (arr[i] < smallest)
smallest = arr[i];
arraySum += arr[i];
}
int minOperation = arraySum -
arr_size * smallest;
Console.Write( "Minimum Operation = " +
minOperation);
}
public static void Main ()
{
int []arr = {5, 6, 2, 4, 3};
printMinOp(arr);
}
}
|
PHP
<?php
function arraySum ( $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $sum += $arr [ $i ++]);
return $sum ;
}
function smallest ( $arr , $n )
{
$small = PHP_INT_MAX;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] < $small )
$small = $arr [ $i ];
return $small ;
}
function minOp ( $arr , $n )
{
$sum = arraySum ( $arr , $n );
$small = smallest ( $arr , $n );
$minOperation = $sum - ( $n * $small );
return $minOperation ;
}
$arr = array (5, 6, 2, 4, 3);
$n = sizeof( $arr );
echo "Minimum Operation = " , minOp ( $arr , $n );
?>
|
Javascript
<script>
function printMinOp(arr)
{
var arraySum, smallest, arr_size = arr.length;
arraySum = 0;
smallest = arr[0];
for (i = 0; i < arr_size; i++)
{
if (arr[i] < smallest)
smallest = arr[i];
arraySum += arr[i];
}
var minOperation = arraySum - arr_size * smallest;
document.write( "Minimum Operation = " + minOperation);
}
var arr = [ 5, 6, 2, 4, 3 ];
printMinOp(arr);
</script>
|
OutputMinimum Operation = 10
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
This article is contributed by Shivam Pradhan . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.