Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | Java program for the above approach ; Function to find the minimum moves ; Stores the sum of the array ; Store the maximum 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
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 ) ; } }