In this HackerRank Utopian Tree problem, 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. A Utopian Tree sapling with a height of 1 meter is planted at the onset of spring. How tall will the tree be after n growth cycles? find the height of the tree after the given number of cycles.

HackerRank Utopian Tree problem solution


Problem solution in Python programming.

def odd(n):
    return ((n % 2) == 1);

def even(n):
    return ((n % 2) == 0);

def utopia(n):
    if n == 0:
        return 1;
    elif odd(n):
        return 2 * utopia(n-1);
    elif even(n):
        return 1 + utopia(n-1);
    

t = int(input())

for i in range(0, t):
    n = int(input())
    print(utopia(n))


Problem solution in Java Programming.

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        final int 
            MONSOON = 100, 
            SUMMER = 200;
        Scanner input = new Scanner(System.in);
        int caseCount = Integer.parseInt(input.nextLine());
        for (int i = 0; i < caseCount; i++) {
            int treeHeight = 1;
            int cycleType = MONSOON;
            int cycleCount = Integer.parseInt(input.nextLine());
            for (int j = 0; j < cycleCount; j++) {
                switch (cycleType) {
                case MONSOON:
                    treeHeight = treeHeight * 2;
                    cycleType = SUMMER;
                    break;
                case SUMMER:
                    treeHeight += 1;
                    cycleType = MONSOON;
                    break;
                }
            }
            System.out.println(treeHeight);
        }
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define FOR(i,s,e) for (int i = int(s); i != int(e); i++)
#define FORIT(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define sz(v) (int)v.size()
#define all(c) (c).begin(), (c).end()

typedef long long int ll;

int main() {
    int t;
    while (scanf("%d", &t) == 1) {
        for (int i = 0; i < t; i++) {
            int n;
            scanf("%d", &n);

            int height = 1;
            for (int j = 0; j < n; j++) {
                if (j % 2 == 0)
                    height *= 2;
                else
                    height++;
            }

            printf("%d\n", height);
        }
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define lld long long int

int main() {
    int T, N;
    scanf("%d\n", &T);
    int i, j;
    
    for (i = 0; i < T; i++) {
        scanf("%d\n", &N);
        lld height = 1;
        
        for (j = 1; j <= N; j++) {
            if (j%2) height *= 2;
            else height++;
        }
        
        printf("%lld\n", height);
    }
    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
	lines = input.split("\n");
    for(var i=1; i<lines.length; i++){
        if(lines[i].length > 0){
            var height=1;
        	for(var j=0; j<lines[i]; j++){
            	if(j%2==0){
                  height = height*2;  
                }else{
                  height = height+1;    
                } 
            }
            console.log(height);
            height=0;
        }
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});