Maximum sum of elements divisible by K from the given array |  ; Function to return the maximum sum divisible by k from elements of v ; check if sum of elements excluding the current one is divisible by k ; check if sum of elements including the current one is divisible by k ; Store the maximum ; Driver code
class GFG { static int [ ] [ ] dp = new int [ 1001 ] [ 1001 ] ; static int find_max ( int i , int sum , int [ ] v , int k ) { if ( i == v . length ) return 0 ; if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; int ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = Math . max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 43 , 1 , 17 , 26 , 15 } ; int k = 16 ; for ( int i = 0 ; i < 1001 ; i ++ ) for ( int j = 0 ; j < 1001 ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . print ( find_max ( 0 , 0 , arr , k ) ) ; } }