Marc's Cakewalk

Sort by

recency

|

434 Discussions

|

  • + 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 comments

    include

    using namespace std; int main(){ long int n; cin >> n; long long int p = 0; int arr[n]; for(int i = 0; i < n; i++){ cin >> arr[i]; }

    // Sort in descending order
    sort(arr, arr + n, greater<int>());
    
    for(int i = 0; i < n; i++){
        p += pow(2, i) * arr[i];
    }
    
    cout << p;
    
    return 0;
    

    }

  • + 0 comments

    My Java solution with o(n log n) time complexity and o(1) space complexity:

    public static long marcsCakewalk(List<Integer> calorie) {
            // Sort the calorie array in descending order
            Collections.sort(calorie, Collections.reverseOrder());
            
            // Calculate the total miles to walk using powers of 2 (bit-shifting)
            long totalMiles = 0;
            for (int i = 0; i < calorie.size(); i++) {
                long currentMilesToWalk = (1L << i) * calorie.get(i); // More efficient power of 2 calculation
                totalMiles += currentMilesToWalk;
            }
            
            return totalMiles;
        }
    
  • + 0 comments
    def marcsCakewalk(calorie):
        return sum(2**i x for i, x in enumerate(sorted(calorie, reverse=True)))
    
  • + 0 comments

    Here is my single line Python solution :

    def marcsCakewalk(calorie):
        # Write your code here    
        return sum((2**i) * calorie[i] for i in range(len(calorie))) if calorie.sort(reverse=True) or True else 0