Marc's Cakewalk

Sort by

recency

|

437 Discussions

|

  • + 0 comments

    Question's explnation is not clear.

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

    }