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; }
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
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