# Ways to sum to N using array elements with repetition allowed

Given a set of m distinct positive integers and a value ‘N’. The problem is to count the total number of ways we can form ‘N’ by doing sum of the array elements. Repetitions and different arrangements are allowed.

**Examples :**

Input : arr = {1, 5, 6}, N = 7 Output : 6 Explanation:- The different ways are:1+1+1+1+1+1+1 1+1+5 1+5+1 5+1+1 1+6 6+1Input : arr = {12, 3, 1, 9}, N = 14 Output : 150

**Approach:** The approach is based on the concept of dynamic programming.

countWays(arr, m, N)Declare and initializecount[N + 1]= {0} count[0] = 1 fori= 1 to N forj= 0 to m - 1 if i >= arr[j] count[i] += count[i - arr[j]] return count[N]

Below is the implementation of above approach.

## C++

*chevron_right*

*filter_none*

## Java

*chevron_right*

*filter_none*

## Python3

*chevron_right*

*filter_none*

## C#

*chevron_right*

*filter_none*

## PHP

*chevron_right*

*filter_none*

**Output:**

Total number of ways = 6

**Time Complexity:** O(N*m)

This article is contributed by **Ayush Jauhari**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Unbounded Knapsack (Repetition of items allowed)
- Number of ways to choose elements from the array such that their average is K
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Number of ways to form an array with distinct adjacent elements
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Move all negative elements to end in order with extra space allowed
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Maximum sum of pairwise product in an array with negative allowed
- Maximum array sum with prefix and suffix multiplications with -1 allowed
- Find a Fixed Point in an array with duplicates allowed
- Check if array contains contiguous integers with duplicates allowed
- Check if it is possible to sort an array with conditional swapping of adjacent allowed
- Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed