Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal
Last Updated :
21 Apr, 2021
Given an array arr[] consisting of N elements, the task is to find the minimum number of increments required to be performed on the given array such that after selecting any array element at any index and splitting its value to the other array elements makes all other N – 1 elements equal.
Examples:
Input: N = 3, arr[] = {2, 3, 7}
Output: 2
Explanation:
Incrementing arr[0] and arr[1] by 1 modifies arr[] to {3, 4, 7}.
Removing arr[0] and adding to arr[1] makes the array {7, 7}.
Removing arr[1] and adding to arr[0] makes the array {7, 7}
Removing arr[2] and adding 3 to arr[1] and 4 to arr[0] makes the array {7, 7}.
Therefore, the count of increments required is 2.
Input: N = 3, arr[] = {0, 2, 0}
Output: 2
Approach: Follow the below steps to solve the problem:
- Find the sum of the given array elements and the maximum element present in that array and store it in variables, say sum and maxelement.
- All remaining N – 1 elements must be equal to ceil(sum / N-1). Let this value be K.
- As the element can only be increased by 1, set K equal to maxelement if maxelement is greater than K.
- Now, each N – 1 value should be equal to K. Therefore, the final sum should be K * (N-1).
- Hence, the total number of moves required is K*(N – 1) – sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumMoves( int arr[], int N)
{
int sum = 0;
int maxelement = -1;
if (N == 2) {
cout << 0;
}
for ( int i = 0; i < N; i++) {
sum += arr[i];
maxelement = max(maxelement, arr[i]);
}
int K = (sum + N - 2) / (N - 1);
K = max(maxelement, K);
int ans = K * (N - 1) - sum;
cout << ans;
}
int main()
{
int arr[] = { 2, 3, 7 };
int N = 3;
minimumMoves(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void minimumMoves(
int [] arr, int N)
{
int sum = 0 ;
int maxelement = - 1 ;
if (N == 2 ) {
System.out.print( "0" );
return ;
}
for ( int i = 0 ; i < N; i++) {
sum += arr[i];
maxelement = Math.max(
maxelement, arr[i]);
}
int k = (sum + N - 2 ) / (N - 1 );
k = Math.max(maxelement, k);
int ans = k * (N - 1 ) - sum;
System.out.println(ans);
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 7 };
int N = arr.length;
minimumMoves(arr, N);
}
}
|
Python3
def minimumMoves(arr, N):
sum = 0
maxelement = - 1
if (N = = 2 ):
print ( 0 , end = "")
for i in range (N):
sum + = arr[i]
maxelement = max (maxelement, arr[i])
K = ( sum + N - 2 ) / / (N - 1 )
K = max (maxelement, K)
ans = K * (N - 1 ) - sum
print (ans)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 7 ]
N = 3
minimumMoves(arr, N)
|
C#
using System;
class GFG
{
static void minimumMoves( int [] arr, int N)
{
int sum = 0;
int maxelement = -1;
if (N == 2)
{
Console.Write( "0" );
return ;
}
for ( int i = 0; i < N; i++)
{
sum += arr[i];
maxelement = Math.Max(
maxelement, arr[i]);
}
int k = (sum + N - 2) / (N - 1);
k = Math.Max(maxelement, k);
int ans = k * (N - 1) - sum;
Console.WriteLine(ans);
}
static void Main()
{
int [] arr = { 2, 3, 7 };
int N = arr.Length;
minimumMoves(arr, N);
}
}
|
Javascript
<script>
function minimumMoves(arr, N)
{
let sum = 0;
let maxelement = -1;
if (N == 2)
{
document.write( "0" );
return ;
}
for (let i = 0; i < N; i++)
{
sum += arr[i];
maxelement = Math.max(
maxelement, arr[i]);
}
let k = (sum + N - 2) / (N - 1);
k = Math.max(maxelement, k);
let ans = k * (N - 1) - sum;
document.write(ans);
}
let arr = [ 2, 3, 7 ];
let N = arr.length;
minimumMoves(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...