Linkavich14
Linkavich14

Reputation: 263

Get all possible combinations to get a given sum with no repeating numbers

I am trying to solve a problem with numbers:

I receive a number and have to calculate the numbers that add up to get that number, there are some rules that make it harder

Rules:

I have been trying to do it like in the knapsack problem but the difference is that I must choose a fixed amount of numbers to get the sum.

If anyone has an idea of an algorithm to fix this, I would really appreciate it.

Upvotes: 0

Views: 5645

Answers (3)

Lim SY
Lim SY

Reputation: 185

Here is the code I come up with in C++ using dynamic programming. n is the maximum number to add. m is the element count and s is the target sum.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int mini(int n, int m) {
    return m * (m + 1) / 2;
}
int maxi(int n, int m) {
    return m * (2 * n - m + 1) / 2;
}

typedef std::vector<unsigned long long> Long1D;
typedef std::vector<Long1D> Long2D;
typedef std::vector<Long2D> Long3D;

int main(int argc, const char * argv[]) {
    int n, m, s;
    n = 45;
    m = 6;
    s = 21;

    if ((s < mini(n, m)) || (s > maxi(n, m))) {
        cout << 0 << endl;
        return 0;
    }

    Long3D dp(2, Long2D(m + 1, Long1D(s + 1)));

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= min(i, m); ++j) {
            for (int k = 1; k <= s; ++k) {
                if ((k < mini(i, j)) || (k > maxi(i, j))) {
                    dp[i % 2][j][k] = 0;
                }
                else if ((k == mini(i, j)) || (k == maxi(i, j)) || j == 1) {
                    dp[i % 2][j][k] = 1;
                }
                else {
                    dp[i % 2][j][k] = 0;
                    // !IMPORTANT -- general situation: dp[i][j][k]=dp[i-1][j-1][k-j]+dp[i-1][j][k-j]
                    if (k - j > mini(i - 1, j - 1))
                        dp[i % 2][j][k] += dp[(i - 1) % 2][j - 1][k - j];
                    if (k - j < maxi(i - 1, j))
                        dp[i % 2][j][k] += dp[(i - 1) % 2][j][k - j];
                }
            }
        }
    }

    cout << dp[n % 2][m][s] << endl;
    return 0;
}

Upvotes: -2

icer
icer

Reputation: 116

In Java: If you need to list the combinations:

static void sumToValue(int limit, int sum, int count, List<Integer> resultIP) {
    if (limit >= 0 && sum == 0 && count == 0) {
        // print resultIP, because it is one of the answers.
        System.out.println("sum(" + Arrays.toString(resultIP.toArray()) + ")");
    } else if (limit <= 0 || count == 0 || sum <= 0) {
        // not what we want
        return;
    } else {
        // Two options: choose current limit number or not
        sumToValue(limit - 1, sum, count, resultIP);// Not choose the limit
                                                    // number

        // or choose the limit number
        List<Integer> resultNext = new ArrayList<Integer>(resultIP);// copy
                                                                    // resultIP
        resultNext.add(limit);
        sumToValue(limit - 1, sum - limit, count - 1, resultNext);
    }
}

If you only need the count:

static void sumToValueCount(int limit, int sum, int count) {
    int dp[][][] = new int[limit + 1][sum + 1][count + 1];
    for (int i = 0; i <= limit; i++) {
        for (int j = 0; j <= sum; j++) {
            for (int k = 0; k <= count; k++) {
                if (j == 0 && k == 0) {
                    dp[i][j][k] = 1;
                } else if (i == 0 || j <= 0 || k == 0) {
                    dp[i][j][k] = 0;
                } else {
                    // check to prevent negative index
                    if (j - i >= 0) {
                        // two options: choose the number or not choose the number
                        dp[i][j][k] = dp[i - 1][j - i][k - 1] + dp[i - 1][j][k];
                    } else {
                        dp[i][j][k] = dp[i - 1][j][k];
                    }
                }
            }
        }
    }
    System.out.println(dp[limit][sum][count]);
}

In main function call like this:

//limit is 45, sum is the sum we want, count is 6 referring to the question.
sumToValue(45, 255, 6, new ArrayList<Integer>());
sumToValueCount(45, 255, 6);

Upvotes: 2

Teguh Suryo Santoso
Teguh Suryo Santoso

Reputation: 171

You can use dynamic programming to solve this problem.

Figure that dp[N][LastNumber][ElementCount] is How many ways to yield N with the last number is LastNumber and the number of element is ElementCount. With N = 1..255, LastNumber = 1..45, ElementCount = 1..6

You can get dp[N][LastNumber][ElementCount] from subsolution dp[N-LastNumber][1][ElementCount-1] + dp[N-LastNumber][2][ElementCount-1] ... + dp[N-LastNumber][LastNumber-1][ElementCount-1]

The base case is dp[i][i][1] = 1 for i = 1..45

if it is asked how many ways to sum up M, the asnwer is dp[M][i][6] for i = 1..45

Upvotes: 4

Related Questions