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.
Laura plants a Utopian Tree sapling with a height of 1 meter at the onset of spring. How tall will her tree be after  growth cycles?
For example, if the number of growth cycles is , the calculations are as follows:
Period          Height
0                                1
1                              2
2                              3
3                            6
4                            7
5                         14
 Description
It should return the integer height of the tree after the input number of growth cycles.
utopianTree has the following parameter(s):
  • n: an integer, the number of growth cycles to simulate

Input Format
The first line contains an integer, , the number of test cases.
 subsequent lines each contain an integer, , denoting the number of cycles for that test case.
Output Format
For each test case, print the height of the Utopian Tree after  cycles. Each height must be printed on a new line.

Question from hackerrank.
Solution in C# -

class Solution {
    static int utopianTree(int n) {
        int h  = 1;
        for(int i=0; i<n; i++)
        {
            if(i%2 == 0)
                h *= 2;
            else
                h += 1;
        }
        return h;
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
        int t = Convert.ToInt32(Console.ReadLine());
        for (int tItr = 0; tItr < t; tItr++) {
            int n = Convert.ToInt32(Console.ReadLine());
            int result = utopianTree(n);
            textWriter.WriteLine(result);
        }
        textWriter.Flush();
        textWriter.Close();
    }
}

Post a Comment

Previous Post Next Post