Java Array Sum sum_configurations(int[] elements, int sum)

Here you can find the source of sum_configurations(int[] elements, int sum)

Description

given a set of integers, returns the number of ways to make them add up to sum, order dependent analogously, 'how many ways can i make change with given coin denominations'

License

Apache License

Declaration

static long sum_configurations(int[] elements, int sum) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*  w w w . j av a 2 s .c o  m*/
     * given a set of integers, returns the number of ways to make them add up
     * to sum, order dependent analogously, 'how many ways can i make change
     * with given coin denominations'
     */
    static long sum_configurations(int[] elements, int sum) {
        long[] arr = new long[sum + 1];
        arr[0] = 1;
        for (int i = 1; i < arr.length; ++i) {
            for (int j : elements)
                if (i - j >= 0)
                    arr[i] += arr[i - j];
        }
        return arr[sum];
    }
}

Related

  1. sum(Object[] srcOne, Object[] srcTwo)
  2. sum(short tab[], int a, int b)
  3. sum(short[] ary)
  4. sum(String[] tokens, int start, int length, String separator)
  5. sum3(double[] a, double[] b)
  6. sumabs(double[] in)
  7. sumandsub(String[] X)
  8. sumArray(double inputArray[])
  9. sumArray(double[] array)