Utopian Tree

Can you predict the height of the tree after N growth cycles? Problem statement here .

Difficulty Level

Cake walk

Required Knowledge

Basic Knowledge of any programming language

Time Complexity

O(N) for each test case

Approach

Let us consider s is height of the tree.

If season is summer one, height will increase by one unit i.e. s++
else season will be monsoon and height will be twice i.e. s = 2*s
and both seasons will come alternatively

Setter’s Code

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int T,N,i;
    cin>>T;
    while(T--)
    {
        cin>>N;
        long long int s = 1;
        for(i=1 ; i<=N ; i++)
        {
                if(i%2 == 0)
                      s++;
                else
                     s = 2*s;
        }
        cout<<s<<endl;
    }
    return 0;
}

Tester’s Code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int pow1(int x, int y){
    int number = 1;
        while (y){
        if (y & 1)  number = number * x;
        y >>= 1;
        x = x * x;
    }
    return number;
}

int fastexp(int a,int n){
    if (n==1) return a;
    if (n%2==0) return (fastexp(a,n/2)*fastexp(a,n/2));
    else return (a*fastexp(a,(n-1)/2)*fastexp(a,(n-1)/2));
}
int main() {
    int t;
    int n,i;
    scanf("%d",&t);
    for (i=0;i<t;i++) {
        scanf("%d",&n);
        printf("%d\n",fastexp(2,(n+1)/2+1)-1-(n%2));
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}