[Solved] Given recursive expression, find algorithm with space complexity O(1) [closed]

This sequence is Stern’s diatomic sequence, and the function you’ve been given is called fusc(n). One way to compute it in O(log n) time and O(1) space complexity is to find the n’th value in the Calkin-Wilf tree. That value’s numerator will be fusc(n). You can read some background on the wikipedia page: https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree Here’s … Read more

[Solved] How to cover a range using a set of ranges with minimal overlap?

Here is the greedy algorithm – always the best place to start. allocate all teams IF not all sections covered output -1 stop mark all teams non-critical flag_improved = true WHILE( flag_improved == true ) flag_improved = false find most expensive section find most expensive non-critical team on most expensive section IF team found that … Read more

[Solved] Given a positive integer N as input, how do I find the product of the numbers in their subsets?

Well, there is a very easy and naive recursive solution to retrieving all subsets. You take away one element from your set, then find all subsets for this new, smaller set. Then you copy the result and add the element you previously removed to the copy. Add the results together and you’re done. For example: … Read more

[Solved] Sum a number with divisors to get another number

You can solve this using dynamic programming #include <iostream> using namespace std; #define INF 1000000007 int main() { // your code goes here int dp[101] = {0}; int a,b,i,j; scanf(“%d%d”,&a, &b); for(i=a;i<=b;i++) dp[i] = INF; dp[a] = 0; for(i=a; i<=b; i++){ for(j=2; j*j<=i; j++){ if(i%j == 0){ dp[i+j] = min(dp[i+j], dp[i]+1); dp[i+i/j] = min(dp[i+i/j], dp[i]+1); … Read more