Today we look at a few more problem solving.
First we look at a balanced partition of an array C of numbers such that the same of the two partitions have very little difference. Typically the partitions have half the total sum The numbers are each in the range 0 < x < k
This is also a dynamic programming problem. We create a boolean array T of size N + 1. T[x] will be true only if and only if there is a subset whose sum = x when we have this boolean array, we take the T[N/2] value. If this is true then there is a subset that adds up to this value.
We take C[i] and look at the table T. When we consider a new number C[i], we could simply ignore the number or include it. It may be possible to make the sum without including it. Now consider some location T[j] that has a one in it. It corresponds to some subset of numbers that adds up to the required sum. When we include C[i], we get a new sum C[i] + j and the corresponding T[j+sum(C[i])] should be set to true as well.
for (int i = 0; i < n; i ++)
for (int j = N - C[i]; j>= 0; j--)
if (T[j] ) then T[j+C[i]] = true;
return T[n/2]
N is the total sum
T is the boolean array with the first element set to True and everything else False.
#codingexercise
Double GetNthRootProductAlternateTermRaisedPPlusQoverPTimesQ (Double [] A,Double p, Double q)
{
If ( A== null) return 0;
Return A.NthRootProductAlternateTermRaisedPPlusQoverPTimesQ(p, q);
}
First we look at a balanced partition of an array C of numbers such that the same of the two partitions have very little difference. Typically the partitions have half the total sum The numbers are each in the range 0 < x < k
This is also a dynamic programming problem. We create a boolean array T of size N + 1. T[x] will be true only if and only if there is a subset whose sum = x when we have this boolean array, we take the T[N/2] value. If this is true then there is a subset that adds up to this value.
We take C[i] and look at the table T. When we consider a new number C[i], we could simply ignore the number or include it. It may be possible to make the sum without including it. Now consider some location T[j] that has a one in it. It corresponds to some subset of numbers that adds up to the required sum. When we include C[i], we get a new sum C[i] + j and the corresponding T[j+sum(C[i])] should be set to true as well.
for (int i = 0; i < n; i ++)
for (int j = N - C[i]; j>= 0; j--)
if (T[j] ) then T[j+C[i]] = true;
return T[n/2]
N is the total sum
T is the boolean array with the first element set to True and everything else False.
#codingexercise
Double GetNthRootProductAlternateTermRaisedPPlusQoverPTimesQ (Double [] A,Double p, Double q)
{
If ( A== null) return 0;
Return A.NthRootProductAlternateTermRaisedPPlusQoverPTimesQ(p, q);
}
No comments:
Post a Comment