HackerRank Utopian Tree Solution

Dec 12, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Utopian Tree Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Utopian Tree Solution

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.
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

Explanation

There are 3 test cases.

In the first case (n = 0), the initial height (H = 1) of the tree remains unchanged.

In the second case (n = 1), the tree doubles in height and is 2 meters tall after the spring cycle.

In the third case (n = 4), the tree doubles its height in spring (n = 1, H = 2), then grows a meter in summer (n = 2, H = 3), then doubles after the next spring (n = 3, H = 6), and grows another meter after summer (n = 4, H = 7). Thus, at the end of 4 cycles, its height is 7 meters.

HackerRank Utopian Tree Solution

Utopian Tree 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 Solution in Cpp

```#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 Solution in Java

```import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Solution
{
public static void main(String[] args) throws NumberFormatException, IOException
{
PrintWriter printWriter = new PrintWriter(System.out);

while(testCount-- > 0)
{
}

printWriter.flush();
}
{
long h = 1;

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

return h;
}
}```

Utopian Tree 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 Solution using JavaScript

```'use strict';
function processData(input) {
var parse_fun = function (s) { return parseInt(s, 10); };
var lines = input.split('\n');
var T = parse_fun(lines.shift());
var data = lines.splice(0, T).map(parse_fun);
for (var t = 0; t < T; t++) {
var N = data[t];
console.log(Math.pow(2, Math.ceil(N / 2) + 1) - 1 + ((N % 2 == 0) ? 0 : -1));
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });```

Utopian Tree Solution in Scala

```object Solution {
def main(args: Array[String]) {
(1 to n).foreach {
_ =>
val r = (1 to t).foldLeft(1) {
case (acc, cycle) => if(cycle % 2 == 1) acc * 2 else acc + 1
}
println(r)
}
}
}```

Utopian Tree Solution in Pascal

```var tt,ii,i,n:longint;
var hts:array[0..60] of int64;
begin
hts[0]:=1;
for i:=1 to 60 do begin
if i mod 2=1 then hts[i]:=2*hts[i-1]
else hts[i]:=1+hts[i-1];
end;
for ii:=1 to tt do begin
writeln(hts[n]);
end;
end.```

Disclaimer: This problem (Utopian Tree) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.