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 = 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++
#include <bits/stdc++.h>
using namespace std;
int minIncrement( int arr[], int N)
{
int mx = 0, sum = 0;
for ( int i = 0; i < N; i++) {
mx = max(mx, arr[i]);
sum += arr[i];
}
int ans = (N - 1) * mx;
if (ans > sum) {
return (ans - 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 == ans) {
return count;
}
else {
return (count + (ans - sum));
}
}
}
int main()
{
int arr[] = { 4, 3, 1, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minIncrement(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minIncrement( int arr[], int N)
{
int mx = 0 , sum = 0 ;
for ( int i = 0 ; i < N; i++) {
mx = Math.max(mx, arr[i]);
sum += arr[i];
}
int ans = (N - 1 ) * mx;
if (ans > sum) {
return (ans - 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 == ans) {
return count;
}
else {
return (count + (ans - sum));
}
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 3 , 1 , 6 };
int N = arr.length;
System.out.print(minIncrement(arr, N));
}
}
|
Python3
def minIncrement(arr, N):
mx = 0
sum = 0
for i in range ( 0 , N):
mx = max (mx, arr[i])
sum + = arr[i]
ans = (N - 1 ) * mx
if (ans > sum ):
return (ans - 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 = = ans):
return count
else :
return (count + (ans - sum ))
if __name__ = = "__main__" :
arr = [ 4 , 3 , 1 , 6 ]
N = len (arr)
print (minIncrement(arr, N))
|
C#
using System;
class GFG {
static int minIncrement( int []arr, int N)
{
int mx = 0, sum = 0;
for ( int i = 0; i < N; i++) {
mx = Math.Max(mx, arr[i]);
sum += arr[i];
}
int ans = (N - 1) * mx;
if (ans > sum) {
return (ans - 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 == ans) {
return count;
}
else {
return (count + (ans - sum));
}
}
}
public static void Main()
{
int []arr = { 4, 3, 1, 6 };
int N = arr.Length;
Console.Write(minIncrement(arr, N));
}
}
|
Javascript
<script>
function minIncrement(arr, N)
{
let mx = 0, sum = 0;
for (let i = 0; i < N; i++) {
mx = Math.max(mx, arr[i]);
sum += arr[i];
}
let ans = (N - 1) * mx;
if (ans > sum) {
return (ans - 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 == ans) {
return count;
}
else {
return (count + (ans - sum));
}
}
}
let arr = [4, 3, 1, 6];
let N = arr.length;
document.write(minIncrement(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
24 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...