![HackerRank Utopian Tree Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Utopian-Tree-Problem-Solution.jpg)
HackerRank Utopian Tree Problem Solution
In this post, We are going to solve 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?
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.
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 Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Utopian-Tree-Problem-Solution-1024x576.jpg)
Utopian Tree C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i, j, k, h, N;
scanf("%d",&k);
for(j=0; j<k; j++)
{
h = 1;
scanf("%d",&N);
for(i=1; i<=N; i++)
{
if(i%2==0)
h++;
else
h*=2;
}
printf("%i\n",h);
}
return 0;
}
Utopian Tree C++ Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,h,cycle; scanf("%d", &t);
for(int cs=1; cs<=t; cs++)
{
scanf("%d", &cycle);
h = 1;
bool doubleLen = false;
for(int i=1; i<=cycle; i++)
{
if(!doubleLen)
{
h*=2; doubleLen = true;
}
else
{
h++; doubleLen = false;
}
}
cout << h << endl;
}
return 0;
}
Utopian Tree C Sharp Solution
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 */
var T = Int32.Parse(Console.ReadLine());
for(int t = 1;t<=T;t++) {
var n = Int32.Parse(Console.ReadLine());
long h = 1;
for(int i = 1;i<=n;i++) {
if(i%2==1)
h*=2;
else
h+=1;
}
Console.WriteLine(h);
}
}
}
Utopian Tree Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'utopianTree' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/
public static int utopianTree(int n) {
// Write your code here
int height = 0;
for(int i = 0; i <= n; i++) {
if(i%2 == 0) {
height++;
} else {
height*=2;
}
}
return height;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
int n = Integer.parseInt(bufferedReader.readLine().trim());
int result = Result.utopianTree(n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Utopian Tree JavaScript Solution
function processData(input) {
input.split('\n').forEach(function(v, i, a){
if(i==0) return;
var h = 1;
for(var j=0; j<v; j++){
if(j%2==0){
h = h*2;
}else{
h++;
}
}
console.log(h);
});
}
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 Python Solution
T = int(input())
test = [None]*T
for j in range(T):
test[j] = int(input())
def find_height(N):
height = 1
for i in range(N):
if i%2 == 0:
height = height*2
elif i%2 == 1:
height = height + 1
return height
for k in range(T):
print(find_height(test[k]))
Other Solutions