The Wayback Machine - https://web.archive.org/web/20241119104602/https://www.geeksforgeeks.org/minimum-number-increment-operations-make-array-elements-equal/
Open In App

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

Last Updated : 19 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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





C++




// [STL] - CPP for finding minimum operation required 
#include<bits/stdc++.h>
using namespace std;
  
// function for finding min operation
int minOp (int arr[], int n)
{
    // find array sum
    int sum = accumulate(arr,arr+n,0);
    // find the smallest element from array
    int small = *min_element(arr,arr+n);
    // calculate min operation required
    int minOperation = sum - (n * small);
    // return result
    return minOperation;
}
  
//driver function
int main()
{
    int arr[] = {5, 6, 2, 4, 3};
    int n = sizeof(arr)/ sizeof(arr[0]);
    cout << "Minimum Operation = " << minOp (arr, n);
    return 0;
} 
  
// This code is contributed by Sania Kumari Gupta


C




// C for finding minimum operation required
#include <limits.h>
#include <stdio.h>
  
// function for finding array sum
int arraySum(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
    return sum;
}
  
// function for finding smallest element
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;
}
  
// function for finding min operation
int minOp(int arr[], int n)
{
    // find array sum
    int sum = arraySum(arr, n);
    // find the smallest element from array
    int small = smallest(arr, n);
    // calculate min operation required
    int minOperation = sum - (n * small);
    // return result
    return minOperation;
}
  
// driver function
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;
}
  
// This code is contributed by Sania Kumari Gupta


Java




// Java program to find min operation
import java.io.*;
  
class minOperation {
    // 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);
    }
}
  
// This code is contributed by Sania Kumari Gupta


Python3




# Python 3 for finding minimum 
# operation required
  
# function for finding min
# operation
def minOp (arr, n) :
      
    # find array sum
    sm = sum(arr)
  
    # find the smallest element from 
    # array
    small = min(arr)
  
    # calculate min operation required
    minOperation = sm - (n * small)
  
    # return result
    return minOperation
      
# Driver function
arr = [5, 6, 2, 4, 3]
n = len(arr)
print( "Minimum Operation = ", minOp (arr, n))
  
# This code is contributed by Shubham Singh


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




<?php
// PHP for finding minimum operation required
  
// function for finding array sum
function arraySum ($arr, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $sum += $arr[$i++]);
    return $sum;
}
  
// function for finding smallest element
function smallest ($arr, $n)
{
    $small = PHP_INT_MAX;
    for ($i = 0; $i < $n; $i++)
        if ($arr[$i] < $small)
            $small = $arr[$i];
    return $small;
}
  
// function for finding min operation
function minOp ($arr, $n)
{
      
    // find array sum
    $sum = arraySum ($arr, $n);
  
    // find the smallest
    // element from array
    $small = smallest ($arr, $n);
  
    // calculate min operation 
    // required
    $minOperation = $sum - ($n * $small);
  
    // return result
    return $minOperation;
}
  
    // Driver Code
    $arr = array (5, 6, 2, 4, 3);
    $n = sizeof($arr);
    echo "Minimum Operation = " , minOp ($arr, $n);
      
// This code is contributed by m_kit
?>


Javascript




<script>
// javascript program to find min operationclass minOeration{
    /*
     * Function to print minimum operation
     required to make all elements of an array
     * equal
     */
    function printMinOp(arr) 
    {
        var arraySum, smallest, arr_size = arr.length;
        arraySum = 0;
        smallest = arr[0];
        for (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];
        }
  
        var minOperation = arraySum - arr_size * smallest;
  
        /* Print min operation required */
        document.write("Minimum Operation = " + minOperation);
  
    }
  
    /* Driver program to test above functions */
        var arr = [ 5, 6, 2, 4, 3 ];
        printMinOp(arr);
  
// This code is contributed by aashish1995
</script>


Output

Minimum 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.

 



Similar Reads

Minimize increment or increment and decrement of Pair to make all Array elements equal
Given an array arr[] of size N, the task is to minimize the number of steps to make all the array elements equal by performing the following operations: Choose an element of the array and increase it by 1.Select two elements simultaneously (arr[i], arr[j]) increase arr[i] by 1 and decrease arr[j] by 1. Examples: Input: arr = [4, 2, 4, 6]Output: 2Ex
8 min read
Count minimum decrement prefix or suffix or increment all operations to make Array equal to 0
Given an array arr[] of size N. The task is to make all the array elements equal to zero by applying the minimum number of operations. Following operations are allowed: Select an index i and decrease each element by 1 for the prefix up to that index.Select an index i and decrease each element by 1 for the suffix starting from that index.Increase al
7 min read
Minimum increment and decrement by K of each pair elements required to make all array elements equal
Given an array arr[], the task is to check if it is possible to make all array elements equal by repeatedly choosing a triplet (i, j, k), where i and j are different, and subtract k from arr[i] and add k to arr[j]. Examples: Input: arr[] = {1, 5, 6, 4}Output: YesExplanation:Operations performed: Choose i = 2, j = 0, k = 2 and perform the given oper
10 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: arr[] = {3, 1, 9, 100}, K = 3 Output: 14 Increment
11 min read
Minimum increment by k operations to make all equal
You are given an array of n-elements, you have to find the number of operations needed to make all elements of array equal. Where a single operation can increment an element by k. If it is not possible to make all elements equal print -1. Example : Input : arr[] = {4, 7, 19, 16}, k = 3Output : 10Input : arr[] = {4, 4, 4, 4}, k = 3Output : 0Input :
6 min read
Minimum number of increment operations required to make two Strings equal
Given two strings S1 and S2 of the same length containing characters ranging from '0' to '9', the task is to return the minimum number of operations required to make two strings equal by following the below operations: Increment any two characters from the string by 1.If it is not possible to make two strings equal with above condition then return
8 min read
Minimum number of increment/decrement operations such that array contains all elements from 1 to N
Given an array of N elements, the task is to convert it into a permutation (Each number from 1 to N occurs exactly once) by using the following operations a minimum number of times: Increment any number.Decrement any number. Examples: Input: arr[] = {1, 1, 4} Output: 2 The array can be converted into [1, 2, 3] by adding 1 to the 1st index i.e. 1 +
4 min read
Minimum Increment or Bitwise OR operations to make two integers equal
Given two integers X and Y (X < Y), we can perform the following operations any number of times: Increment X by 1, X = X + 1Increment Y by 1, Y = Y + 1 Change X to X | Y, X = X | YThe task is to determine the minimum number of operations required to make X = Y. Input: X = 1, Y = 3 Output: 1 Explanation: It takes only one operation to make X = Y,
8 min read
Minimum Increment / decrement to make array elements equal
Given an array of integers where [Tex]1 \leq A[i] \leq 10^{18} [/Tex]. In one operation you can either Increment/Decrement any element by 1. The task is to find the minimum operations needed to be performed on the array elements to make all array elements equal. Examples: Input : A[] = { 1, 5, 7, 10 } Output : 11 Increment 1 by 4, 5 by 0. Decrement
7 min read
Minimum cost to make Array equal by increment/decrementing elements
Given an array, arr[], and the cost array cost[], the task is to find the minimum cost to make all the array elements equal by incrementing or decrementing the element at any index by 1. The cost of the increment or decrement operation for the element at index i is the cost[i]. Examples: Input: arr[] = {1, 3, 5, 2}, cost[] = {2, 3, 1, 14}Output: 8E
8 min read
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) such that |i - j| = 1 (indices i and j are adjacent
6 min read
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other
Given an array arr[] and an integer K, the task is to find the minimum number of operations to make all the elements of the array arr[] equal. In one operation, K is subtracted from an element and this K is added into other element. If it is not possible then print -1. Examples: Input: arr[] = {5, 8, 11}, K = 3Output: 1Explanation:Operation 1: Subt
7 min read
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements
Given an array arr[] of size N, the task is to make all array elements equal to 0 by replacing all elements of a subsequences of equal elements by any integer, minimum number of times. Examples: Input: arr[] = {3, 7, 3}, N = 3Output: 2Explanation:Selecting a subsequence { 7 } and replacing all its elements by 0 modifies arr[] to { 3, 3, 3 }. Select
5 min read
Minimum increments done in Array such that arr[i] can make all other elements equal
Given an array arr[] of N size, the task is to find the minimum increment required to elements in the array such that if any element says arr[i] is distributed among the others make N - 1 element equal. Examples: Input: arr[] = {0, 0, 3}, N = 3Output: 3Explanation: Increments can be done in the following way: Increment element at index 0 by 1, so a
8 min read
Minimum increment operations to make the array in increasing order
Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array. Examples: Input : a = { 1, 3, 3, 2 }, X = 2 Output : 3 Explanation : Modified array is { 1, 3, 5, 6 } Input : a = { 3, 5, 6 }, X = 5 Output : 0 Observation: Let's take two numbers a and b. a >
7 min read
Minimum increment or decrement operations required to make the array sorted
Given an array arr[] of N integers, the task is to sort the array in non-decreasing order by performing the minimum number of operations. In a single operation, an element of the array can either be incremented or decremented by 1. Print the minimum number of operations required.Examples: Input: arr[] = {1, 2, 1, 4, 3} Output: 2 Add 1 to the 3rd el
15+ min read
Minimize increment or decrement operations to make given Array elements consecutive
Given an array, arr[] of size N., The task is to perform minimum increment or decrement operations on the elements of the array, to make all the elements consecutive, Output the minimum sum of all the possible changes(addition and subtractions) required to do the same. Examples: Input: N = 5, arr[] = {13, 6, 11, 18, 4}Output: 15Explanation: Convert
5 min read
Make all array elements equal by reducing array elements to half minimum number of times
Given an array arr[] consisting of N integers, the task is to minimize the number of operations required to make all array elements equal by converting Ai to Ai / 2. in each operation Examples: Input: arr[] = {3, 1, 1, 3}Output: 2Explanation: Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}. Reducing A3 to A3 / 2 modifies arr[] to {1, 1, 1, 1}.
6 min read
Minimum number of bits of array elements required to be flipped to make all array elements equal
Given an array arr[] consisting of N positive integers, the task is to find the minimum number of bits of array elements required to be flipped to make all array elements equal. Examples: Input: arr[] = {3, 5}Output: 2Explanation:Following are the flipping of bits of the array elements required: For element arr[0](= 3): Flipping the 3rd bit from th
7 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. Examples: Input: N = 3, M = 2, arr[] = {1, 2, 3}Ou
5 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 deleted (the size of the array remains same). If th
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[] = {5, 7, 10, 5, 15}, B[] = {2, 2, 1, 3, 5} Output:
7 min read
Minimum Cost to make all array elements equal using given operations
Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is to find the minimum total cost to make all the elem
12 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: 2Explanation:Initially, the array is {1, 3, 5}. Following
10 min read
Minimum operations required to make all elements of Array less than equal to 0
Given an array arr[] consisting of N positive numbers, the task is to find the minimum number of operations required to make all elements of the array less than or equal to 0. In each operation, one has to pick the minimum positive element from the array and subtract all the elements of the array from that number. Examples: Input: arr[] = {1, 2, 4,
5 min read
Minimize absolute difference between the smallest and largest array elements by minimum increment decrement operations
Given an array arr[] consisting of N positive integers, the task is to minimize the number of operations required to minimize the absolute difference between the smallest and largest elements present in the array. In each operation, subtract 1 from an array element and increment 1 to another array element. Examples: Input: arr[] = {1, 6}Output: 2Ex
8 min read
Minimum increment/decrement operations required to make Median as X
Given an array A[] of n odd integers and an integer X. Calculate the minimum number of operations required to make the median of the array equal to X, where, in one operation we can either increase or decrease any single element by one. Examples: Input: A[] = {6, 5, 8}, X = 8 Output: 2 Explanation: Here 6 can be increased twice. The array will beco
6 min read
Maximize length of subarray of equal elements by performing at most K increment operations
Given an array A[] consisting of N integers and an integer K, the task is to maximize the length of the subarray having equal elements after performing at most K increments by 1 on array elements. Note: Same array element can be incremented more than once. Examples: Input: A[] = {2, 4, 8, 5, 9, 6}, K = 6Output: 3Explanation:Subarray [8, 5, 9] can b
8 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: arr[] = {99, 96, 97, 95}Output: 3Explanation:Operat
7 min read
Minimum sum of values subtracted from array elements to make all array elements equal
Given an array arr[] consisting of N positive integers, the task is to find the sum of all the array elements required to be subtracted from each array element such that remaining array elements are all equal. Examples: Input: arr[] = {1, 2}Output: 1Explanation: Subtracting 1 from arr[1] modifies arr[] to {1, 1}. Therefore, the required sum is 1. I
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg