Minimum number of increment, We’ve been given an array of n components. You can choose any one element and increase the rest of the n-1 elements by one at each operation. You must make all elements equal and replicate the process as many times as you like. Determine the smallest number of operations required.

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``````

Java Solution :

``````// 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);
}
} ``````

Also read : Tips to Learn Programming Faster

Tagged: