We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Utopian Tree
  5. Discussions

Utopian Tree

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 1757 Discussions, By:

recency

Please Login in order to post a comment

  • Dhroov
    2 hours ago+ 0 comments

    RECURSION

    int utopianTree(int n) { if(n==0) return 1; if(n%2==0) return 1 + utopianTree(n-1); return 2 * utopianTree(n-1); }

    0|
    Permalink
  • radoslawtadeusi1
    8 hours ago+ 0 comments

    c#

     int height = 1;
            for (int i = 1; i <= n; i++)
            {
                if (i % 2 == 1) // nieparzyste
                {
                    height *= 2;
                }
                else // parzyste
                {
                    height += 1;
                }
            }
            return height;
    
    0|
    Permalink
  • minkhantmgmg_mk1
    1 day ago+ 0 comments
    def utopianTree(n):
        h = 1
        for i in range(n):
            if i % 2 == 0:
                h *= 2
            else:
                h += 1
    
        return h
    
    0|
    Permalink
  • ghatge25
    2 days ago+ 0 comments

    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
    
    ArrayList arr=new ArrayList<Integer>();
    
    
    int count=0;
    
    for(int i=0;i<=n;i++)
    {
        if(i%2==0)
        {
            count+=1;
            arr.add(i, count);
        }
        else
        {
            count=count*2;
            arr.add(i, count);
        }
    }
    
    int wawa=(int)arr.get(n);
    return wawa;
    
    
    }
    

    }

    0|
    Permalink
  • gonczy_balazs95
    3 days ago+ 0 comments

    Simple no brainer Python 3 solution:

    def utopianTree(n):
        count = 0
        for i in range(n+1):
            if i == 0:
               count += 1
            elif i % 2 == 0:
                count += 1
            else:
                count *= 2
        return count
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy