Sort by

recency

|

1996 Discussions

|

  • + 0 comments

    Utopian Tree sounds like a really interesting concept! I love how it brings a unique vibe to the space. Speaking of unique, if you're ever in need of a Hummer repair, I know some great places to check out! Keep creating cool ideas like this!

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/0G-kEeQC25E

    Solution 1 : O(N)

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

    Solutoin 2 : O(1)

    int utopianTree(int n) {
        string s(n / 2 + 1, '1');
        if(n % 2 == 1) s += '0';
        bitset<60> ans(s); 
        return ans.to_ullong();
    }
    

    Solutoin 3 : O(1)

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

    Solutoin 4 : O(1)

    int utopianTree(int n) {
        return ((1 << ((n / 2) + 1) ) - 1) << n%2;
    }
    
  • + 0 comments

    public static int utopianTree(int n) { // Write your code here int output = 0;

        for(int i = 0; i <= n; i++){
            if (i % 2 == 0 || i == 0) output = output + 1;
            else output = output + output;
        }
        return output;
    }
    
  • + 0 comments
        public static int utopianTree(int n)
        {
            var height = 1;
           for(int i = 0; i < n; i++) {
               if(i % 2 == 0) {
                   height *= 2;
               } else {
                   height += 1;
               }
           } 
           
           return height;
        }
    
  • + 1 comment
    def utopianTree(n):
        k = n // 2
        return ((n % 2) + 1) *  (2 ** (k + 1) - 1)
    

    Clean python solution