Find all money sums that can be made from given coins

Updated

Given n coins with certain values, the task is to find all the money sums you can create using these coins.

Examples :

Input : 3
        1 3 7
Output : 7
	1 3 4 7 8 10 11
Explanation :
Combination of given coins Sum 1                   1
3                   3
1,3                 4
7                   7
1,7                 8
3,7                 10
1,3,7               11

Input : 4
	4 2 5 2
Output : 9
	2 4 5 6 7 8 9 11 13
Explanation :
Combination of given coins Sum 2                     2
4                     4
5                     5
2,4                   6
5,2                   7
4,2,2                 8
5,4                   9
5,4,2                 11
5,4,2,2               13

Approach :

The problem can be solved by using the 2D DP approach. We know the maximum possible sum can be the sum of all given values. So, we can create a boolean two-dimensional array. The number of columns of the array can be equal to the maximum sum possible and the number of rows can be equal to the number of coins given. While traversing in the array we have to check whether we can make the sum of coins equal to the current column number. If yes we can store true at that place else false will be stored. At last, we can traverse in the last row of the array and if the current position of the array has a value of boolean true we can print the current column number. This is how we can print all possible sums.

Algorithm :

  1. Create a variable n to store the number of coins. Then create an array of size n to store values of coins.
  2. Create a two-dimensional boolean array with the number of rows equal to n+1 and columns equal to maximum sum+1.
  3. In the 0th row place false in every column since with 0 we cannot make any possible sum and in the 0th column place true in every row since every number can make sum = 0.
  4. Now start traversing in the array and check if the current column value can be expressed as the sum of given coins.
  5. Create a count variable and initialize it to 0.
  6. Now start traversing in the last row and check if the current position in the array has true or false stored in it. Increment count by 1 for every true value.
  7. Print count and again start traversing in the last row to print the possible sum values.

Implementation :

// C++ program for the above approach
#include <iostream>
using namespace std;


// Function to calculate money sum
void calculateSum(int n, int coins[]) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += coins[i];
    }
    bool dp[n + 1][sum + 1];

    for (int i = 0; i <= sum; i++) {
        dp[0][i] = false;
    }
    for (int i = 0; i <= n; i++) {
        dp[i][0] = true;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= sum; j++) {
            if (j >= coins[i - 1]) {
                dp[i][j] = dp[i - 1][j] || dp[i - 1][j - coins[i - 1]];
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    int count = 0;

    for (int i = 1; i <= sum; i++) {
        if (dp[n][i] == true) {
            count++;
        }
    }
    cout << count << "\n";
    for (int i = 1; i <= sum; i++) {
        if (dp[n][i] == true) {
            cout << i << " ";
        }
    }
}



// Driver Code
int main() {

    int n = 4;
    int coins[4] = {4, 2, 5, 2};
  
  	// Function call
    calculateSum(n, coins);

    return 0;
}
Output :

9
2 4 5 6 7 8 9 11 13

Time Complexity : O(n * sum)

Comment
Next tutorial
Mithlesh Upadhyay Published 24 Nov 2026 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials