Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | 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 ; Given array ; Size of given array ; Function Call
function minimumMoves ( arr , N ) { let sum = 0 ; let maxelement = - 1 ; if ( N == 2 ) { document . write ( " " ) ; 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 ) ;