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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to count minimum moves void minimumMoves( int arr[], int N)
{ // Stores sum of given array
int sum = 0;
// Stores maximum array element
int maxelement = -1;
// Base Case
if (N == 2) {
// If N is 2, the answer
// will always be 0
cout << 0;
}
// Traverse the array
for ( int i = 0; i < N; i++) {
// Calculate sum of the array
sum += arr[i];
// Finding maximum element
maxelement = max(maxelement, arr[i]);
}
// Calculate ceil(sum/N-1)
int K = (sum + N - 2) / (N - 1);
// If k is smaller than maxelement
K = max(maxelement, K);
// Final sum - original sum
int ans = K * (N - 1) - sum;
// Print the minimum number
// of increments required
cout << ans;
} // Driver Code int main()
{ // Given array
int arr[] = { 2, 3, 7 };
// Size of given array
int N = 3;
// Function Call
minimumMoves(arr, N);
return 0;
} |
// Java program for the above approach import java.io.*;
class GFG {
// Function to find the minimum moves
public static void minimumMoves(
int [] arr, int N)
{
// Stores the sum of the array
int sum = 0 ;
// Store the maximum element
int maxelement = - 1 ;
// Base Case
if (N == 2 ) {
// If N is 2, the answer
// will always be 0
System.out.print( "0" );
return ;
}
// Traverse the array
for ( int i = 0 ; i < N; i++) {
// Calculate sum of the array
sum += arr[i];
// Finding maximum element
maxelement = Math.max(
maxelement, arr[i]);
}
// Calculate ceil(sum/N-1)
int k = (sum + N - 2 ) / (N - 1 );
// If k is smaller than maxelement
k = Math.max(maxelement, k);
// Final sum - original sum
int ans = k * (N - 1 ) - sum;
// Print the minimum number
// of increments required
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int [] arr = { 2 , 3 , 7 };
// Size of given array
int N = arr.length;
// Function Call
minimumMoves(arr, N);
}
} |
# Python3 program for the above approach # Function to count minimum moves def minimumMoves(arr, N):
# Stores sum of given array
sum = 0
# Stores maximum array element
maxelement = - 1
# Base Case
if (N = = 2 ):
# If N is 2, the answer
# will always be 0
print ( 0 , end = "")
# Traverse the array
for i in range (N):
# Calculate sum of the array
sum + = arr[i]
# Finding maximum element
maxelement = max (maxelement, arr[i])
# Calculate ceil(sum/N-1)
K = ( sum + N - 2 ) / / (N - 1 )
# If k is smaller than maxelement
K = max (maxelement, K)
# Final sum - original sum
ans = K * (N - 1 ) - sum
# Print the minimum number
# of increments required
print (ans)
# Driver Code if __name__ = = '__main__' :
# Given array
arr = [ 2 , 3 , 7 ]
# Size of given array
N = 3
# Function Call
minimumMoves(arr, N)
# This code is contributed by mohit kumar 29 |
// C# program for the above approach using System;
class GFG
{ // Function to find the minimum moves
static void minimumMoves( int [] arr, int N)
{
// Stores the sum of the array
int sum = 0;
// Store the maximum element
int maxelement = -1;
// Base Case
if (N == 2)
{
// If N is 2, the answer
// will always be 0
Console.Write( "0" );
return ;
}
// Traverse the array
for ( int i = 0; i < N; i++)
{
// Calculate sum of the array
sum += arr[i];
// Finding maximum element
maxelement = Math.Max(
maxelement, arr[i]);
}
// Calculate ceil(sum/N-1)
int k = (sum + N - 2) / (N - 1);
// If k is smaller than maxelement
k = Math.Max(maxelement, k);
// Final sum - original sum
int ans = k * (N - 1) - sum;
// Print the minimum number
// of increments required
Console.WriteLine(ans);
}
// Driver code
static void Main()
{
// Given array
int [] arr = { 2, 3, 7 };
// Size of given array
int N = arr.Length;
// Function Call
minimumMoves(arr, N);
}
} // This code is contributed by divyesh072019. |
<script> // Javascript program for the above approach
// Function to find the minimum moves
function minimumMoves(arr, N)
{
// Stores the sum of the array
let sum = 0;
// Store the maximum element
let maxelement = -1;
// Base Case
if (N == 2)
{
// If N is 2, the answer
// will always be 0
document.write( "0" );
return ;
}
// Traverse the array
for (let i = 0; i < N; i++)
{
// Calculate sum of the array
sum += arr[i];
// Finding maximum element
maxelement = Math.max(
maxelement, arr[i]);
}
// Calculate ceil(sum/N-1)
let k = (sum + N - 2) / (N - 1);
// If k is smaller than maxelement
k = Math.max(maxelement, k);
// Final sum - original sum
let ans = k * (N - 1) - sum;
// Print the minimum number
// of increments required
document.write(ans);
}
// Given array
let arr = [ 2, 3, 7 ];
// Size of given array
let N = arr.length;
// Function Call
minimumMoves(arr, N);
</script> |
2
Time Complexity: O(N)
Auxiliary Space: O(N)