Maximize array sum by X increments when each element is divided by 10 | Java 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
import java . util . * ; class GFG { public static void maximizeval10 ( int [ ] a , int n , int k ) { int increments = 0 ; int ans = 0 ; Vector < Integer > v = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . add ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( ( a [ i ] ) / 10 ) - 1 ) ; } } Collections . sort ( v ) ; int sum = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { sum += v . get ( i ) ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { int remaining = k - sum ; ans += Math . min ( increments , remaining / 10 ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 4 ; int X = 6 ; int A [ ] = { 4 , 8 , 8 , 8 } ; maximizeval10 ( A , N , X ) ; } }