Calculate the Nth term

Sort by

recency

|

822 Discussions

|

  • + 0 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.

    int find_nth_term(int n, int a, int b, int c) {
        //Write your code here.
        int *cache = malloc(sizeof(int) * (n + 1));
        cache[1] = a;
        cache[2] = b;
        cache[3] = c;
        
        if (n <= 3) return cache[n];
        
        for (int i = 4; i <= n; i++) {
            cache[i] = cache[i - 1] + cache[i - 2] + cache[i - 3];
        }
        
      
        int ret = cache[n];
        free(cache);
        return ret;
    }
    
  • + 0 comments

    Here is Calculate the Nth term solution in c - https://programmingoneonone.com/hackerrank-calculate-the-nth-term-solution-in-c.html

  • + 0 comments

    int find_nth_term(int n, int a, int b, int c) { int res = (a+b)+c; n += res; return (n);
    }
    its execute success but other test scenarios failed. Why? Any idea

  • + 0 comments
    int find_nth_term(int n, int a, int b, int c) {
      //Write your code here.
        if(n==3){
            return c;
        }else{
            return find_nth_term(n-1, b, c, a+b+c);
        }
    
    }
    
  • + 0 comments

    include

    include

    include

    include

    //Complete the following function.

    int find_nth_term(int n, int a, int b, int c) { //Write your code here. int ret = 0; int d = 0;

    if(n == 1)
    {
        ret = a;
    }
    else if(n == 2)
    {
        ret = b;
    }
    else if(n == 3)
    {
        ret = c;
    }else{
        d = c + b + a;       
        ret = find_nth_term(n - 1, b, c, d);
    }
    
    return ret;
    

    }

    int main() { int n, a, b, c;

    scanf("%d %d %d %d", &n, &a, &b, &c);
    int ans = find_nth_term(n, a, b, c);
    
    printf("%d", ans); 
    return 0;
    

    }