C Is the number of calls to PARTITION a good indicator for t
C++
Is the number of calls to PARTITION a good indicator for the running time of the algorithm variants? Why, or why not?
Solution
Solution to use algorithm variants is to find all the different partitions.more specifically we want to use decide and conquer method. So first of all we need to break the problem in to smaller sub problems.
We can apply this split recursively and we will break the problem down in to many sub problems.
When sum equals to zero it means we just reached sum exactly.so the function returns 1. If sum goes below zero it means the last available number was larger than what neefed so function returns 0.
# include<studio.h>
INT partition (INT sum,INT largeno)
{
If(largeno==0)
Return 0;
If(sum==0)
Return 1;
If(sum<1)
Return 0;
Return partition (sum,largeno-1+partition(sum-largeno,largeno);
}
INT main()
{
INT sum=100;
INT largeno=99;
Cout<<partition (sum,largeno);
Return 0;
}
