# Maximum sum of elements divisible by K from the given array

Given an array of integers and a number K. The task is to find the maximum sum which is divisible by K from the given array.

Examples:

Input: arr[] = {3, 6, 5, 1, 8}, k = 3
Output: 18
Explanation: 18 is formed by the elements 3, 6, 1, 8.

Input: arr = { 43, 1, 17, 26, 15 } , k = 16
Output: 32
Explanation: 32 is formed by the elements 17, 15.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: Recursively check all the possible combinations to find the solution. The solution is of exponential time complexity and thus inefficient.

Efficient Approach: A dynamic programming approach by maintaining a 2-D array dp which stores the state of variable sum and i (where sum is the current sum and i is the ith index of integer array). By recursing over all elements, calculate the sum including the element at index i as well as excluding it and check if divisible by k. If so, store the maximum of them in dp[i][sum] and return.

Below code is the implementation of the above approach:

## CPP

 `#include ` `using` `namespace` `std; ` ` `  `int` `dp; ` ` `  `// Function to return the maximum sum ` `// divisible by k from elements of v ` `int` `find_max(``int` `i, ``int` `sum, vector<``int``>& v,``int` `k) ` `{ ` ` `  `    ``if` `(i == v.size()) ` `        ``return` `0; ` ` `  `    ``if` `(dp[i][sum] != -1) ` `        ``return` `dp[i][sum]; ` ` `  `    ``int` `ans = 0; ` `    ``// check if sum of elements excluding the ` `    ``// current one is divisible by k ` `    ``if` `((sum + find_max(i + 1, sum, v, k)) % k == 0) ` `        ``ans = find_max(i + 1, sum, v, k); ` `     `  `    ``// check if sum of elements including the ` `    ``// current one is divisible by k ` `    ``if``((sum + v[i] + find_max(i + 1,(sum + v[i]) % k, ` `                                   ``v, k)) % k == 0) ` `        ``// Store the maximum ` `        ``ans = max(ans, v[i] + find_max(i + 1, ` `                            ``(sum + v[i]) % k,v, k)); ` `     `  ` `  `    ``return` `dp[i][sum] = ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``> arr = { 43, 1, 17, 26, 15 }; ` `    ``int` `k = 16; ` `    ``memset``(dp, -1, ``sizeof``(dp)); ` `    ``cout << find_max(0, 0, arr, k); ` `} `

## Java

 `class` `GFG{ ` `  `  `static` `int` `[][]dp = ``new` `int``[``1001``][``1001``]; ` `  `  `// Function to return the maximum sum ` `// divisible by k from elements of v ` `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``; ` ` `  `    ``// check if sum of elements excluding the ` `    ``// current one is divisible by k ` `    ``if` `((sum + find_max(i + ``1``, sum, v, k)) % k == ``0``) ` `        ``ans = find_max(i + ``1``, sum, v, k); ` `      `  `    ``// check if sum of elements including the ` `    ``// current one is divisible by k ` `    ``if``((sum + v[i] + find_max(i + ``1``,(sum + v[i]) % k, ` `                                   ``v, k)) % k == ``0``) ` `        ``// Store the maximum ` `        ``ans = Math.max(ans, v[i] + find_max(i + ``1``, ` `                            ``(sum + v[i]) % k, v, k)); ` `      `  `    ``return` `dp[i][sum] = ans; ` `} ` `  `  `// Driver code ` `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)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python 3

 `# Python3 implementation ` `dp ``=` `[[``-``1` `for` `i ``in` `range``(``1001``)] ``for` `j ``in` `range``(``1001``)] ` ` `  `# Function to return the maximum sum ` `# divisible by k from elements of v ` `def` `find_max(i, ``sum``, v, k): ` `    ``if` `(i ``=``=` `len``(v)): ` `        ``return` `0` ` `  `    ``if` `(dp[i][``sum``] !``=` `-``1``): ` `        ``return` `dp[i][``sum``] ` ` `  `    ``ans ``=` `0` `     `  `    ``# check if sum of elements excluding the ` `    ``# current one is divisible by k ` `    ``if` `((``sum` `+` `find_max(i ``+` `1``, ``sum``, v, k)) ``%` `k ``=``=` `0``): ` `        ``ans ``=` `find_max(i ``+` `1``, ``sum``, v, k) ` `     `  `    ``# check if sum of elements including the ` `    ``# current one is divisible by k ` `    ``if``((``sum` `+` `v[i] ``+` `find_max(i ``+` `1``,(``sum` `+` `v[i]) ``%` `k, v, k)) ``%` `k ``=``=` `0``): ` `         `  `        ``# Store the maximum ` `        ``ans ``=` `max``(ans, v[i] ``+` `find_max(i ``+` `1``,(``sum` `+` `v[i]) ``%` `k, v, k)) ` `     `  `    ``dp[i][``sum``] ``=` `ans ` ` `  `    ``return` `dp[i][``sum``] ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``43``, ``1``, ``17``, ``26``, ``15``] ` `    ``k ``=` `16` `    ``print``(find_max(``0``, ``0``, arr, k)) ` ` `  `# This code is contributed by Surendra_Gangwar `

## C#

 `using` `System; ` ` `  `class` `GFG{ ` `   `  `static` `int` `[,]dp = ``new` `int``[1001,1001]; ` `   `  `// Function to return the maximum sum ` `// divisible by k from elements of v ` `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; ` `  `  `    ``// check if sum of elements excluding the ` `    ``// current one is divisible by k ` `    ``if` `((sum + find_max(i + 1, sum, v, k)) % k == 0) ` `        ``ans = find_max(i + 1, sum, v, k); ` `       `  `    ``// check if sum of elements including the ` `    ``// current one is divisible by k ` `    ``if``((sum + v[i] + find_max(i + 1,(sum + v[i]) % k, ` `                                   ``v, k)) % k == 0) ` `        ``// Store the maximum ` `        ``ans = Math.Max(ans, v[i] + find_max(i + 1, ` `                            ``(sum + v[i]) % k, v, k)); ` `       `  `    ``return` `dp[i, sum] = ans; ` `} ` `   `  `// Driver code ` `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; ` `    ``Console.Write(find_max(0, 0, arr, k)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```32
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.