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 →

  • oscar0812torres
    2 months ago+ 1 comment

    Java (math):

        public static int utopianTree(int n) {
            int closestOdd = n % 2 == 0 ? n + 1: n;
            int i = ((closestOdd-1)/2) + 2;
            int res = (int)Math.pow(2, i) - 2;
            
            if(n%2 == 0) {
                res /= 2;
            }
            
            return res;
        }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy