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+1
Input: 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 initialize count[N + 1] = {0}
count[0] = 1
for i = 1 to N
for j = 0 to m - 1
if i >= arr[j]
count[i] += count[i - arr[j]]
return count[N]
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int arr[], int m, int N)
{
int count[N + 1];
memset (count, 0, sizeof (count));
count[0] = 1;
for ( int i = 1; i <= N; i++)
for ( int j = 0; j < m; j++)
if (i >= arr[j])
count[i] += count[i - arr[j]];
return count[N];
}
int main()
{
int arr[] = {1, 5, 6};
int m = sizeof (arr) / sizeof (arr[0]);
int N = 7;
cout << "Total number of ways = "
<< countWays(arr, m, N);
return 0;
}
|
Java
class Gfg
{
static int arr[] = { 1 , 5 , 6 };
static int countWays( int N)
{
int count[] = new int [N + 1 ];
count[ 0 ] = 1 ;
for ( int i = 1 ; i <= N; i++)
for ( int j = 0 ; j < arr.length; j++)
if (i >= arr[j])
count[i] += count[i - arr[j]];
return count[N];
}
public static void main(String[] args)
{
int N = 7 ;
System.out.println( "Total number of ways = "
+ countWays(N));
}
}
|
Python3
def countWays(arr, m, N):
count = [ 0 for i in range (N + 1 )]
count[ 0 ] = 1
for i in range ( 1 , N + 1 ):
for j in range (m):
if (i > = arr[j]):
count[i] + = count[i - arr[j]]
return count[N]
arr = [ 1 , 5 , 6 ]
m = len (arr)
N = 7
print ( "Total number of ways = " ,
countWays(arr, m, N))
|
C#
using System;
class Gfg
{
static int []arr = {1, 5, 6};
static int countWays( int N)
{
int []count = new int [N+1];
count[0] = 1;
for ( int i = 1; i <= N; i++)
for ( int j = 0; j < arr.Length; j++)
if (i >= arr[j])
count[i] += count[i - arr[j]];
return count[N];
}
public static void Main()
{
int N = 7;
Console.Write( "Total number of ways = "
+ countWays(N));
}
}
|
PHP
<?php
function countWays( $arr , $m , $N )
{
$count = array_fill (0, $N + 1,0);
$count [0] = 1;
for ( $i = 1; $i <= $N ; $i ++)
for ( $j = 0; $j < $m ; $j ++)
if ( $i >= $arr [ $j ])
$count [ $i ] += $count [ $i - $arr [ $j ]];
return $count [ $N ];
}
$arr = array (1, 5, 6);
$m = count ( $arr );
$N = 7;
echo "Total number of ways = " ,countWays( $arr , $m , $N );
?>
|
Javascript
<script>
let arr = [1, 5, 6];
function countWays(N)
{
let count = new Array(N+1);
count.fill(0);
count[0] = 1;
for (let i = 1; i <= N; i++)
for (let j = 0; j < arr.length; j++)
if (i >= arr[j])
count[i] += count[i - arr[j]];
return count[N];
}
let N = 7;
document.write( "Total number of ways = " + countWays(N));
</script>
|
OutputTotal number of ways = 6
Time Complexity: O(N*m)
Auxiliary Space: O(N), as an additional array of size (N + 1) is required.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.