Maximize array sum by X increments when each element is divided by 10 | Javascript program for the above approach ; Initialize variables ; Add the current contribution of the element to the answer ; If the value is already maximum then we can 't change it ; Moves required to move to the next multiple of 10 ; No of times we can add 10 to this value so that its value does not exceed 1000. ; Sort the array ; Adding the values to increase the numbers to the next multiple of 10 ; If the total moves are less than X then increase the answer ; If the moves exceed X then we cannot increase numbers ; If there still remain some moves ; Remaining moves ; Add minimim of increments and remaining / 10 to the answer ; Output the final answer ; Driver code
function maximizeval10 ( a , n , k ) { let increments = 0 ; let ans = 0 ; let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { ans += Math . floor ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . push ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( Math . floor ( a [ i ] ) / 10 ) - 1 ) ; } } v . sort ( function ( a , b ) { return a - b ; } ) ; let sum = 0 ; for ( let i = 0 ; i < v . length ; i ++ ) { sum += v [ i ] ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { let remaining = k - sum ; ans += Math . min ( increments , Math . floor ( remaining / 10 ) ) ; } document . write ( ans ) ; } let N = 4 ; let X = 6 ; let A = [ 4 , 8 , 8 , 8 ] ; maximizeval10 ( A , N , X ) ;