Utopian Tree HackerRank Solution in C, C++, Java, Python

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?

For example, if the number of growth cycles is n=5, the calculations are as follows:

Period  Height

0          1

1          2

2          3

3          6

4          7

5          14

 

Function Description

Complete the utopianTree function in the editor below.

utopianTree has the following parameter(s):

  • int n: the number of growth cycles to simulate

Returns

  • int: the height of the tree after the given number of cycles

Input Format

The first line contains an integer,t , the number of test cases.

t subsequent lines each contain an integer,n , the number of cycles for that test case.

Constraints

1<=t<=10

0<=n<=60

Sample Input

3

0

1

4

Sample Output

1

2

7

Utopian Tree HackerRank Solution in C

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

int main() {

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

 

Utopian Tree HackerRank Solution in C++

#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;
}

 

Utopian Tree HackerRank Solution in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Solution
{
    public static void main(String[] args) throws NumberFormatException, IOException
    {

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter printWriter = new PrintWriter(System.out);
        
        int testCount = Integer.parseInt(bufferedReader.readLine());
        
        while(testCount-- > 0)
        {
            printWriter.println(getAnswer(Integer.parseInt(bufferedReader.readLine())));
        }
        
        printWriter.flush();
    }

    private static long getAnswer(int n)
    {
        long h = 1;
        
        for(int i = 1 ; i <= n ; i++)
        {
            if(i % 2 != 0)
                h *= 2;
            else
                h +=1 ;
        }
        
        return h;
    }
}

 

Utopian Tree HackerRank Solution in Python

for i in range(input()):
    n=input()
    h=1
    for i in range(1,n+1):
        if i&1:h*=2
        else:h+=1
    print h

 

Utopian Tree HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        
        int T=Convert.ToInt32(Console.ReadLine());
        
        for (int i=0;i<T;i++){
            int N=Convert.ToInt32(Console.ReadLine());
            
            int rez=1;
            bool pomnozi=true;
            
            while(N>0){
                if (pomnozi){
                    rez*=2;
                }
                else{
                    rez++;
                }
                
                pomnozi=!pomnozi;
                N--;
            }
            
            Console.WriteLine(rez.ToString());
        }
    }
}

 

 

Attempt –  HackerRank Challenge

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

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/angry-professor-hackerrank-solution/

Leave a Comment