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
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Utopian Tree
  5. Discussions

Utopian Tree

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

    You are viewing a single comment's thread. Return to all comments →

  • shariq_ansari
    2 months ago+ 0 comments

    A solution in Python, with some comments.

    def utopianTree(n):
        # Set the initial height of the tree
        height = 1
        # Let's setup a range that'll cover every cycle and iterate through it.
        for cycle in range(0, n + 1):
            # Nothing happens during the first cycle, so we skip the rest of the loop.
            if cycle == 0: continue
            # Odd cycles are 'spring', so we add height to itself, otherwise add 1.
            height += height if cycle % 2 == 1 else 1
        return height
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy