Marc's Cakewalk

  • + 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);