Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Utopian Tree problem solution

YASH PAL, 31 July 202430 November 2025

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

Hackerrank Utopian Tree solution in Python.

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("%dn", 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("%dn", &T);
    int i, j;
    
    for (i = 0; i < T; i++) {
        scanf("%dn", &N);
        lld height = 1;
        
        for (j = 1; j <= N; j++) {
            if (j%2) height *= 2;
            else height++;
        }
        
        printf("%lldn", 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);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes