Marc's Cakewalk

Sort by

recency

|

436 Discussions

|

  • + 0 comments
    public static long marcsCakewalk(List<Integer> calorie) {
        Comparator<Integer> comp = Integer::compare;
        List<Integer> cals = calorie.stream().sorted(comp.reversed()).collect(Collectors.toList());
        double mincal = 0;
        for(int i=0;i<cals.size();i++) {
            mincal +=  Math.pow(2, i)*cals.get(i);
        }        
        return (long)mincal;
    }
    
  • + 0 comments

    c++ answer------- long marcsCakewalk(vector calorie) { sort(calorie.begin(),calorie.end(),greater()); long miles=0; for(int i=0;i

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