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. Greedy
  4. Marc's Cakewalk
  5. Discussions

Marc's Cakewalk

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 394 Discussions, By:

recency

Please Login in order to post a comment

  • TuanBao
    5 days ago+ 0 comments

    This is my java 8 solution, feel free to ask me any questions.
    First look

    public static long marcsCakewalk(List<Integer> calorie) {
        //Perform sort the given calories
        calorie.sort(null);
        calorie.sort(Collections.reverseOrder());
        
        long sumCalories = 0;
        
        //Calculate sum calories
        for(int i = 0; i < calorie.size(); i++) {
            long current = (long)Math.pow(2, i) * calorie.get(i);
            sumCalories = sumCalories + current;
        }
    
        return sumCalories;    
    }
    

    Second look (little bit changed using bitwise manipulation)

    long current = (1L << i) * calorie.get(i);
    
    0|
    Permalink
  • nqb11295
    1 week ago+ 0 comments

    This is my solution in JavaScript

    function marcsCakewalk(calorie) {
        // Write your code here
        var result = 0
        calorie.sort((a, b) => (a > b ? -1 : 1))
        for(var i = 0;i < calorie.length;i++){
            result += Math.pow(2,i)*calorie[i] 
        }
        return result
    }
    
    0|
    Permalink
  • alban_tyrex
    3 weeks ago+ 0 comments

    Here is my c++ solution, you can watch explanation here : https://youtu.be/RRJoL2vDmZM

    long marcsCakewalk(vector<int> calorie) {
        sort(calorie.begin(), calorie.end(), [](int l, int r){return l > r;});
        long ans = 0;
        for(int i = 0; i < calorie.size(); i++){
            ans += pow(2, i) * calorie[i];
        }
        return ans;
    }
    
    0|
    Permalink
  • alban_tyrex
    2 months ago+ 0 comments

    Here is my c++ solution, you can watch explanation here : https://youtu.be/RRJoL2vDmZM

    long marcsCakewalk(vector<int> calorie) {
        sort(calorie.begin(), calorie.end(), [](int l, int r){return l > r;});
        long ans = 0;
        for(int i = 0; i < calorie.size(); i++){
            ans += pow(2, i) * calorie[i];
        }
        return ans;
    }
    
    0|
    Permalink
  • tiagoiesbick
    2 months ago+ 0 comments

    Python 3

    def marcsCakewalk(calorie):    
        return sum([(2**i)*w for i,w in enumerate(sorted(calorie, reverse=True))])
    
    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