Search This Blog

Monday, March 3, 2014

Algorithms - Warmup - Utopian Tree

Utopian Tree

The following is the solution to Hacker Rank problem Utopian Tree using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.


Score: 20/20
/**
 *
 */


import java.util.Scanner;

/**
 * @author Arun.G
 *
 */
public class Solution{

       /**
        * @param args
        */
       private static int UtopianHeight(int noOfCycles)
    {
    int height=1;
    if(noOfCycles<=0)
        return 1; // no change of height
    for(int i=1;i<=noOfCycles;i++)
        {
        if(i%2!=0)
            height=2*height; //doubles in odd cycles
        else
            height+=1; // increases by 1 in even cycles
    }
    return height;
}
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int noOfTestCases = sc.nextInt();
        for(int i=0;i<noOfTestCases;i++)
            {
              int noOfCycles = sc.nextInt();
            System.out.println(UtopianHeight(noOfCycles));
        }
        sc.close(); //close scanner
       }

}


No comments:

Post a Comment

Labels