In this post, we will solve HackerRank Marc’s Cakewalk Problem Solution.
Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and Marc can walk a distance to expend those calories. If Marc has eaten j cupcakes so far. after eating a cupcake with e calories he must walk at least 2 x c miles to maintain his weight.
Example
calorie = [5, 10, 7]
If he eats the cupcakes in the order shown, the miles he will need to walk are (20 x 5) + (2¹ x 10)+(22 x 7)=5+20 +28 = 53. This is not the minimum, though, so we need to test other orders of consumption. In this case, our minimum miles is
calculated as (20 x 10) + (21 × 7) + (22 x 5) = 10+14+20= 44.
Given the individual calorie counts for each of the cupcakes, determine the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order.
Function Description
Complete the marcsCakewalk function in the editor below, marcsCakewalk has the following parameter(s):
- int calorie[n]: the calorie counts for each cupcake
Returns
- long: the minimum miles necessary
Input Format
The first line contains an integer n, the number of cupcakes in calorie .
The second line contains n space-separated integers, calorie [i].
Sample Input 0
3
1 3 2
Sample Output 0
11


Marc’s Cakewalk C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int compare(const void *a,const void *b){
return *(int *)b -*(int *)a;
}
int main(){
int n;
scanf("%d",&n);
int *calories = malloc(sizeof(int) * n);
for(int calories_i = 0; calories_i < n; calories_i++){
scanf("%d",&calories[calories_i]);
}
qsort(calories,n,sizeof(int),compare);
long long int sum=0;
for(int i=0;i<n;i++){
sum+=calories[i]*pow(2,i);
}
printf("%lld",sum);
// your code goes here
return 0;
}
Marc’s Cakewalk C++ Solution
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define INF 10000000000000LL
#define P system("pause")
#define print(v) for(typeof(v.begin()) itr = v.begin();itr!=v.end();itr++)cout<<*itr<<" "
#define IN freopen("input.txt","r",stdin)
#define OUT freopen("output.txt","w",stdout)
#define IO IN;OUT
typedef long long ll;
using namespace std;
int main()
{
//IN;
ll n,sum=0,temp;
vector<ll> v;
cin>>n;
for(ll i=0;i<n;i++)
{
cin>>temp;
v.pb(temp);
}
sort(all(v));
reverse(all(v));
for(ll i=0;i<n;i++)
{
sum += pow(2,i)*v[i];
}
cout<<sum;
return 0;
}
Marc’s Cakewalk C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;
using static System.Array;
class Solution {
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] calories_temp = Console.ReadLine().Split(' ');
int[] calories = Array.ConvertAll(calories_temp,Int32.Parse);
// your code goes here
double sum=0;
calories = calories.OrderByDescending(c => c).ToArray();
for(int i=0;i<n;i++)
{
sum=sum+ (calories[i]*Math.Pow(2,i));
}
Console.WriteLine(sum.ToString());
}
}
Marc’s Cakewalk Java Solution
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class A {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] a = na(n);
Arrays.sort(a);
long ret = 0;
for(int v : a){
ret = ret * 2 + v;
}
out.println(ret);
}
void run() throws Exception
{
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new A().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
Marc’s Cakewalk JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(readLine());
calories = readLine().split(' ');
calories = calories.map(Number);
// your code goes here
calories.sort((a, b) => b - a);
//console.log(calories)
let count = 0;
console.log(calories.reduce((sum, el) => {
return sum + (el * Math.pow(2, count++));
}, 0))
}
Marc’s Cakewalk Python Solution
#!/bin/python3
import sys
n = int(input().strip())
calories = list(map(int, input().strip().split(' ')))
calories.sort(reverse = True)
cnt, ans = 0, 0
for c in calories :
ans += c * 2 ** cnt
cnt += 1
print(ans)