## Saturday, April 9, 2016

### Utopian Tree - HackerRank

https://www.hackerrank.com/challenges/utopian-tree

The Utopian Tree goes through 2 cycles of growth every year. Each spring, it doubles in height. Each summer, its height increases by 1 meter.
Laura plants a Utopian Tree sapling with a height of 1 meter at the onset of spring. How tall will her tree be after $N$ growth cycles?
http://aruncyberspace.blogspot.com/2014/03/algorithms-warmup-utopian-tree.html
private static int UtopianHeight(int noOfCycles)
{
int height=1;
if(noOfCycles<=0)
return 1; // no change of height
for(int i=1;i<=noOfCycles;i++)
{
if(i%2!=0)
height=2*height; //doubles in odd cycles
else
height+=1; // increases by 1 in even cycles
}
return height;
}
https://sisijava.wordpress.com/2014/07/14/hackerrank-utopian-tree/
    public static int height(int N){
        int h = 1;
        if(N==0) return h;
        while(N>0){
            if(N>0){ // not needed
                h = h*2;
                N--;
            }
            if(N>0){
                h = h+1;
                N--;
            }
        }
        return h;
    }