Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | C ++ program for the above approach ; Function to count minimum moves ; Stores sum of given array ; Stores maximum array element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver Code ; Given array ; Size of given array ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }