int coin_count(int s, int m) { if (s == 0) return 1; if (s < 0) return 0; if (m < 0 && s >=1) return 0; return coin_count(s, m-1) + coin_count(s - num[m], m); }
Since order does not matter, we will impose that our solutions (sets) are all sorted in non-decreasing order (Thus, we are looking at sorted-set solutions: collections).
For a particular N and S = \{ S_1, S_2, \ldots, S_m \} (now with the restriction that S_1 < S_2 < \ldots < S_m, our solutions can be constructed in non-decreasing order), the set of solutions for this problem, C(N,m), can be partitioned into two sets:
There are those sets that does not contain any Sm, Those sets that contain at least 1 Sm,
If a solution does not contain Sm, then we can solve the subproblem of N with S = \{ S_1, S_2, \ldots, S_{m-1} \}, or the solutions of C(N,m - 1).
If a solution does contain Sm, then we are using at least one Sm, thus we are now solving the subproblem of N - Sm, S = \{ S_1, S_2, \ldots, S_m \}. This is C(N - Sm,m).
Thus, we can formulate the following:
C(N,m) = C(N,m - 1) + C(N - Sm,m)
with the base cases:
C(N,m) = 1,N = 0 C(N,m) = 0,N < 0 C( N, m ) = 0, N \geq 1, m \leq 0
We can also use backtracking. //Assume that input array is sorted in non-increasing order. Please check it's complexity.I am not sure. int subsetSum(int arr[],int len,int value,int i ) { int tmp=0; if( i >=len) return 0; if( value== arr[i] ) { cout<<" "< arr[i]){ tmp=subsetSum(arr,len,value-arr[i], i ); if(tmp) cout<<" "<<arr[i]; else tmp=subsetSum(arr,len,value, i +1); } return tmp; }
Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
int coin_count(int s, int m)
ReplyDelete{
if (s == 0)
return 1;
if (s < 0)
return 0;
if (m < 0 && s >=1)
return 0;
return coin_count(s, m-1) + coin_count(s - num[m], m);
}
Since order does not matter, we will impose that our solutions (sets) are all sorted in non-decreasing order (Thus, we are looking at sorted-set solutions: collections).
ReplyDeleteFor a particular N and S = \{ S_1, S_2, \ldots, S_m \} (now with the restriction that S_1 < S_2 < \ldots < S_m, our solutions can be constructed in non-decreasing order), the set of solutions for this problem, C(N,m), can be partitioned into two sets:
There are those sets that does not contain any Sm,
Those sets that contain at least 1 Sm,
If a solution does not contain Sm, then we can solve the subproblem of N with S = \{ S_1, S_2, \ldots, S_{m-1} \}, or the solutions of C(N,m - 1).
If a solution does contain Sm, then we are using at least one Sm, thus we are now solving the subproblem of N - Sm, S = \{ S_1, S_2, \ldots, S_m \}. This is C(N - Sm,m).
Thus, we can formulate the following:
C(N,m) = C(N,m - 1) + C(N - Sm,m)
with the base cases:
C(N,m) = 1,N = 0
C(N,m) = 0,N < 0
C( N, m ) = 0, N \geq 1, m \leq 0
We can also use backtracking.
ReplyDelete//Assume that input array is sorted in non-increasing order.
Please check it's complexity.I am not sure.
int subsetSum(int arr[],int len,int value,int i )
{
int tmp=0;
if( i >=len) return 0;
if( value== arr[i] )
{
cout<<" "< arr[i]){
tmp=subsetSum(arr,len,value-arr[i], i );
if(tmp)
cout<<" "<<arr[i];
else
tmp=subsetSum(arr,len,value, i +1);
}
return tmp;
}
http://codepad.org/iHVDNvHr