We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
While the recursive solution is relatively trivial, the optimal solution is actually not recursive at all, and instead uses a cache in order to stop having to recalculate the same position every time you need it.
intfind_nth_term(intn,inta,intb,intc){//Write your code here.int*cache=malloc(sizeof(int)*(n+1));cache[1]=a;cache[2]=b;cache[3]=c;if(n<=3)returncache[n];for(inti=4;i<=n;i++){cache[i]=cache[i-1]+cache[i-2]+cache[i-3];}intret=cache[n];free(cache);returnret;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Calculate the Nth term
You are viewing a single comment's thread. Return to all comments →
While the recursive solution is relatively trivial, the optimal solution is actually not recursive at all, and instead uses a cache in order to stop having to recalculate the same position every time you need it.