Skip to content
Related Articles

Related Articles

Minimum increments done in Array such that arr[i] can make all other elements equal

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 24 Dec, 2021
Improve Article
Save Article

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 = 3
Output: 3
Explanation: Increments can be done in the following way:

  • Increment element at index 0 by 1, so arr[] becomes {1, 0, 3}
  • Increment element at index 1 by 2. so arr[] becomes {1, 2, 3}

Now, if any element is chosen and distributed in others, makes all N – 1 elements equal.

Lets say,  

  • choose 1, add it to 2, so N – 1 elements become {3, 3}
  • choose 2, add it to 1, so N – 1 elements become {3, 3}
  • choose 3, add 2 of 3 to 1 and add 1 of 3 to 2, so N – 1 elements become {3, 3}

So, total increments = (1 + 2) = 3

Input: arr[] = {4, 3, 1, 6}, N = 4
Output:  4

 

Approach: The idea to solve this problem is based on the observation that if (N-1)*mx where mx is the maximum element of the array is greater than the sum of the array then the answer would be simply (N-1)*mx – sum. Else if (N-1)*mx is less or equal to sum then take a temporary variable say temp assign it with sum / N – 1. If sum mod N -1 is not equal to 0 increment temp by 1 and take a counter to say count, where count is the difference of temp and mx. Update ans = (count + mx) * (N – 1) and increment sum by count.If sum is equal to (N-1)*mx return count, else return count incremented with the difference of (N-1)*mx and sum. Follow the steps below to solve the problem:

  • Initialize the variables mx and sum as 0 to find the sum and the maximum element of the array.
  • Iterate over the range [0, N) using the variable i and perform the following tasks:
    • Update the value of mx as the maximum of mx or arr[i] and add the value of arr[i] to the variable sum.
  • Initialize the variable ans as (N-1)*mx.
  • If ans is greater than sum then return ans-sum as the answer.
  • Else, initialize the variable temp as sum/(N-1) + sum%(N-1).
  • Initialize the variable count as temp – mx.
  • Set the value of ans as (count + mx)*(N-1).
  • Add the value of count to the variable sum.
  •  If sum equals ans, then return the value of count as the answer else return count + ans – sum.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
int minIncrement(int arr[], int N)
{
    // Variable for max element
    // and sum
    int mx = 0, sum = 0;
    for (int i = 0; i < N; i++) {
        mx = max(mx, arr[i]);
        sum += arr[i];
    }
 
    // Calculate ans
    int ans = (N - 1) * mx;
 
    // If ans is greater than sum
    // return its difference
    if (ans > sum) {
        return (ans - sum);
    }
 
    // If ans is less or equal to sum
    else {
        int temp = sum / (N - 1);
        if (sum % (N - 1) != 0) {
            temp++;
        }
        int count = temp - mx;
        ans = (count + mx) * (N - 1);
        sum += count;
 
        // If sum is equal to ans
        // return the count
        if (sum == ans) {
            return count;
        }
 
        // Else return the summation
        // of count and difference
        // of ans and sum
        else {
            return (count + (ans - sum));
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 3, 1, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << minIncrement(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
static int minIncrement(int arr[], int N)
{
   
    // Variable for max element
    // and sum
    int mx = 0, sum = 0;
    for (int i = 0; i < N; i++) {
        mx = Math.max(mx, arr[i]);
        sum += arr[i];
    }
 
    // Calculate ans
    int ans = (N - 1) * mx;
 
    // If ans is greater than sum
    // return its difference
    if (ans > sum) {
        return (ans - sum);
    }
 
    // If ans is less or equal to sum
    else {
        int temp = sum / (N - 1);
        if (sum % (N - 1) != 0) {
            temp++;
        }
        int count = temp - mx;
        ans = (count + mx) * (N - 1);
        sum += count;
 
        // If sum is equal to ans
        // return the count
        if (sum == ans) {
            return count;
        }
 
        // Else return the summation
        // of count and difference
        // of ans and sum
        else {
            return (count + (ans - sum));
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 4, 3, 1, 6 };
    int N = arr.length;
 
    System.out.print(minIncrement(arr, N));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3




# python program for the above approach
 
# Function to find minimum increment
# required to an element to make N-1
# elements equal by distributing
# any element
def minIncrement(arr, N):
 
        # Variable for max element
        # and sum
    mx = 0
    sum = 0
    for i in range(0, N):
        mx = max(mx, arr[i])
        sum += arr[i]
 
        # Calculate ans
    ans = (N - 1) * mx
 
    # If ans is greater than sum
    # return its difference
    if (ans > sum):
        return (ans - sum)
 
        # If ans is less or equal to sum
    else:
        temp = sum // (N - 1)
        if (sum % (N - 1) != 0):
            temp += 1
 
        count = temp - mx
        ans = (count + mx) * (N - 1)
        sum += count
 
        # If sum is equal to ans
        # return the count
        if (sum == ans):
            return count
 
            # Else return the summation
            # of count and difference
            # of ans and sum
        else:
            return (count + (ans - sum))
 
# Driver Code
if __name__ == "__main__":
 
    arr = [4, 3, 1, 6]
    N = len(arr)
 
    print(minIncrement(arr, N))
 
    # This code is contributed by rakeshsahni


C#




// C# program for the above approach
using System;
class GFG {
 
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
static int minIncrement(int []arr, int N)
{
   
    // Variable for max element
    // and sum
    int mx = 0, sum = 0;
    for (int i = 0; i < N; i++) {
        mx = Math.Max(mx, arr[i]);
        sum += arr[i];
    }
 
    // Calculate ans
    int ans = (N - 1) * mx;
 
    // If ans is greater than sum
    // return its difference
    if (ans > sum) {
        return (ans - sum);
    }
 
    // If ans is less or equal to sum
    else {
        int temp = sum / (N - 1);
        if (sum % (N - 1) != 0) {
            temp++;
        }
        int count = temp - mx;
        ans = (count + mx) * (N - 1);
        sum += count;
 
        // If sum is equal to ans
        // return the count
        if (sum == ans) {
            return count;
        }
 
        // Else return the summation
        // of count and difference
        // of ans and sum
        else {
            return (count + (ans - sum));
        }
    }
}
 
// Driver Code
public static void Main()
{
    int []arr = { 4, 3, 1, 6 };
    int N = arr.Length;
 
    Console.Write(minIncrement(arr, N));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
 
        // JavaScript Program to implement
        // the above approach
 
        // Function to find minimum increment
        // required to an element to make N-1
        // elements equal by distributing
        // any element
        function minIncrement(arr, N)
        {
         
            // Variable for max element
            // and sum
            let mx = 0, sum = 0;
            for (let i = 0; i < N; i++) {
                mx = Math.max(mx, arr[i]);
                sum += arr[i];
            }
 
            // Calculate ans
            let ans = (N - 1) * mx;
 
            // If ans is greater than sum
            // return its difference
            if (ans > sum) {
                return (ans - sum);
            }
 
            // If ans is less or equal to sum
            else {
                let temp = sum / (N - 1);
                if (sum % (N - 1) != 0) {
                    temp++;
                }
                let count = temp - mx;
                ans = (count + mx) * (N - 1);
                sum += count;
 
                // If sum is equal to ans
                // return the count
                if (sum == ans) {
                    return count;
                }
 
                // Else return the summation
                // of count and difference
                // of ans and sum
                else {
                    return (count + (ans - sum));
                }
            }
        }
 
        // Driver Code
        let arr = [4, 3, 1, 6];
        let N = arr.length;
 
        document.write(minIncrement(arr, N));
 
    // This code is contributed by Potta Lokesh
    </script>


 
 

Output

4

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!