Related Articles

# Find maximum subset-sum divisible by D by taking at most K elements from given array

• Last Updated : 05 Aug, 2021

Given an array A[] of size N, and two numbers K and D, the task is to calculate the maximum subset-sum divisible by D possible by taking at most K elements from A.

Examples:

Input: A={11, 5, 5, 1, 18}, N=5, K=3, D=7
Output:
28
Explanation:
The subset {5, 5, 18} gives the maximum sum=(5+5+18)=28 that is divisible by 7 and also has contains atmost 3 elements

Input: A={7, 7, 7, 7, 7}, N=5, K=2, D=7
Output:
14

Naive Approach: The Naive approach would be to generate all subsets of A(using bit masking), and for each subset, calculate the sum, and check whether the length of the subset is not greater than K, and the sum is divisible by D, and calculating the maximum among them.

Below is the implementation of the above approach:

## C++

 `// C++ program for tha above approach``#include ``using` `namespace` `std;` `// Function to calculate maximum sum possible by taking at``// most K elements that is divisibly by D``int` `maximumSum(vector<``int``> A, ``int` `N, ``int` `K, ``int` `D)``{``    ``// variable to store final answer``    ``int` `ans = 0;``    ``// Traverse all subsets``    ``for` `(``int` `i = 0; i < (1 << N); i++) {``        ``int` `sum = 0;``        ``int` `c = 0;``        ``for` `(``int` `j = 0; j < N; j++) {``            ``if` `(i >> j & 1) {``                ``sum += A[j];``                ``c++;``            ``}``        ``}``        ``// Update ans if necessary``        ``// conditions are satisfied``        ``if` `(sum % D == 0 && c <= K)``            ``ans = max(ans, sum);``    ``}``    ``return` `ans;``}``// Driver code``int` `main()``{``    ``// Input``    ``int` `N = 5, K = 3, D = 7;``    ``vector<``int``> A = { 1, 11, 5, 5, 18 };` `    ``// Function call``    ``cout << maximumSum(A, N, K, D) << endl;` `    ``return` `0;``}`

## Java

 `// Java program for tha above approach``import` `java.util.*;` `class` `GFG{` `// Function to calculate maximum sum``// possible by taking at most K``// elements that is divisibly by D``static` `int` `maximumSum(``int``[] A, ``int` `N,``                      ``int` `K, ``int` `D)``{``    ` `    ``// Variable to store final answer``    ``int` `ans = ``0``;``    ` `    ``// Traverse all subsets``    ``for``(``int` `i = ``0``; i < (``1` `<< N); i++)``    ``{``        ``int` `sum = ``0``;``        ``int` `c = ``0``;``        ``for``(``int` `j = ``0``; j < N; j++)``        ``{``            ``if` `((i >> j & ``1``) != ``0``)``            ``{``                ``sum += A[j];``                ``c++;``            ``}``        ``}``        ` `        ``// Update ans if necessary``        ``// conditions are satisfied``        ``if` `(sum % D == ``0` `&& c <= K)``            ``ans = Math.max(ans, sum);``    ``}``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Input``    ``int` `N = ``5``, K = ``3``, D = ``7``;``    ``int``[] A = { ``1``, ``11``, ``5``, ``5``, ``18` `};` `    ``// Function call``    ``System.out.print(maximumSum(A, N, K, D));``}   ``}` `// This code is contributed by susmitakundugoaldanga`

## Python3

 `# Python3 program for tha above approach` `# Function to calculate maximum sum``# possible by taking at most K elements``# that is divisibly by D``def` `maximumSum(A, N, K, D):``    ` `    ``# Variable to store final answer``    ``ans ``=` `0``    ` `    ``# Traverse all subsets``    ``for` `i ``in` `range``((``1` `<< N)):``        ``sum` `=` `0``        ``c ``=` `0``        ` `        ``for` `j ``in` `range``(N):``            ``if` `(i >> j & ``1``):``                ``sum` `+``=` `A[j]``                ``c ``+``=` `1``                ` `        ``# Update ans if necessary``        ``# conditions are satisfied``        ``if` `(``sum` `%` `D ``=``=` `0` `and` `c <``=` `K):``            ``ans ``=` `max``(ans, ``sum``)``            ` `    ``return` `ans` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Input``    ``N ``=` `5``    ``K ``=` `3``    ``D ``=` `7``    ``A ``=` `[ ``1``, ``11``, ``5``, ``5``, ``18` `]` `    ``# Function call``    ``print``(maximumSum(A, N, K, D))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for tha above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{`` ` `// Function to calculate maximum sum possible``// by taking at most K elements that is divisibly by D``static` `int` `maximumSum(List<``int``> A, ``int` `N,``                           ``int` `K, ``int` `D)``{``    ` `    ``// Variable to store final answer``    ``int` `ans = 0;``    ` `    ``// Traverse all subsets``    ``for``(``int` `i = 0; i < (1 << N); i++)``    ``{``        ``int` `sum = 0;``        ``int` `c = 0;``        ``for``(``int` `j = 0; j < N; j++)``        ``{``            ``if` `((i >> j & 1) != 0)``            ``{``                ``sum += A[j];``                ``c++;``            ``}``        ``}``        ` `        ``// Update ans if necessary``        ``// conditions are satisfied``        ``if` `(sum % D == 0 && c <= K)``            ``ans = Math.Max(ans, sum);``    ``}``    ``return` `ans;``}` `// Driver code``public` `static` `void` `Main()``{``    ` `    ``// Input``    ``int` `N = 5, K = 3, D = 7;``    ``List<``int``> A = ``new` `List<``int``>(){ 1, 11, 5, 5, 18 };` `    ``// Function call``    ``Console.Write(maximumSum(A, N, K, D));``}``}` `// This code is contributed by SURENDRA_GANGWAR`

## Javascript

 ``
Output:
`28`

Time Complexity: O(N.2N)
Auxiliary Space: O(1)

Efficient Approach: This problem can be solved with the help of dynamic programming, with a 3D dp array, where dp[i][j][p] stores the maximum sum possible if j elements are taken till the ith index and its modulo D is p. Follow the steps to solve the problem:

1. Create a 3D array dp[][][] of size (N+1)x(K+1)x(D), and initialize it with -1.
2. Iterate from 1 to N, and for each current index i, do the following:
1. Initialize two variables element and mod to A[i-1] and A[i-1]%D.
2. Copy dp[i-1] to dp[i].
3. Iterate from 1 to K, and for each current index j, do the following:
1. Update dp[i][j][mod] as the maximum of dp[i][j][mod] and element.
2. Iterate from 0 to D-1, and for each current index p, do the following:
1. If dp[i-1][j-1][p] is not equal to -1, Update dp[i][j][(p+mod)%D] as the maximum of dp[i][j][(p+mod)%D] and dp[i-1][j-1][p]+element.
3. If dp[N][K] is -1, the answer is 0.
4. Otherwise, the answer is dp[N][K].

Below is the implementation of the above approach:

## C++

 `#include ``using` `namespace` `std;``int` `maximumSum(vector<``int``> A, ``int` `N, ``int` `K, ``int` `D)``{``    ``// Dp vector``    ``vector > > dp(``        ``N + 1, vector >(``                   ``K + 1, vector<``int``>(D + 1, -1)));``    ``for` `(``int` `i = 1; i <= N; i++) {``        ``// current element``        ``int` `element = A[i - 1];``        ``// current element modulo D``        ``int` `mod = A[i - 1] % D;``        ``// copy previous state``        ``dp[i] = dp[i - 1];``        ``for` `(``int` `j = 1; j <= K; j++) {``            ``// Transitions``            ``dp[i][j][mod] = max(dp[i][j][mod], element);``            ``for` `(``int` `p = 0; p < D; p++) {``                ``if` `(dp[i - 1][j - 1][p] != -1) {``                    ``dp[i][j][(p + mod) % D] = max(``                        ``dp[i][j][(p + mod) % D],``                        ``dp[i - 1][j - 1][p] + element);``                ``}``            ``}``        ``}``    ``}``    ``// return answer``    ``if` `(dp[N][K] == -1)``        ``return` `0;``    ``return` `dp[N][K];``}``// Driver code``int` `main()``{``    ``// Input``    ``int` `N = 5, K = 3, D = 7;``    ``vector<``int``> A = { 1, 11, 5, 5, 18 };` `    ``// Function call``    ``cout << maximumSum(A, N, K, D) << endl;` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG {` `static` `int` `maximumSum(``int``[] A, ``int` `N, ``int` `K, ``int` `D)``{``  ` `    ``// Dp vector``    ``int``[][][] dp = ``new` `int``[N+``1``][K+``1``][D+``1``];``    ` `    ``for``(``int` `i = ``0``; i < N + ``1``; i++)``    ``{``        ``for``(``int` `j = ``0``; j < K + ``1``; j++)``        ``{``            ``for``(``int` `k = ``0``; k < D + ``1``; k++)``            ``{``                ``dp[i][j][k] = -``1``;``            ``}``        ``}``    ``}` `    ``for` `(``int` `i = ``1``; i <= N; i++)``    ``{``      ` `        ``// current element``        ``int` `element = A[i - ``1``];``      ` `        ``// current element modulo D``        ``int` `mod = A[i - ``1``] % D;``      ` `        ``// copy previous state``        ``dp[i] = dp[i - ``1``];``        ``for` `(``int` `j = ``1``; j <= K; j++)``        ``{``          ` `            ``// Transitions``            ``dp[i][j][mod] = Math.max(dp[i][j][mod], element);``            ``for` `(``int` `p = ``0``; p < D; p++) {``                ``if` `(dp[i - ``1``][j - ``1``][p] != -``1``) {``                    ``dp[i][j][(p + mod) % D] = Math.max(``                        ``dp[i][j][(p + mod) % D],``                        ``dp[i - ``1``][j - ``1``][p] + element);``                ``}``            ``}``        ``}``    ``}``  ` `    ``// return answer``    ``if` `(dp[N][K][``0``] == -``1``)``        ``return` `0``;``    ``return` `dp[N][K][``0``];``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``// Input``    ``int` `N = ``5``, K = ``3``, D = ``7``;``    ``int``[] A = { ``1``, ``11``, ``5``, ``5``, ``18` `};` `    ``// Function call``    ``System.out.print(maximumSum(A, N, K, D));``}``}` `// This code is contributed by SURENDRA_GANGWAR.`
Output:
`28`

Time Complexity: O(NKD)
Auxiliary Space: O(NKD)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up