Hackerrank - Marc's Cakewalk Solution
1 min read

Hackerrank - Marc's Cakewalk Solution

Hackerrank - Marc's Cakewalk 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  cupcakes so far, after eating a cupcake with  calories he must walk at least  miles to maintain his weight.

For example, if he eats  cupcakes with calorie counts in the following order: , the miles he will need to walk are . This is not the minimum, though, so we need to test other orders of consumption. In this case, our minimum miles is calculated as .

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. It should return a long integer that represents the minimum miles necessary.

marcsCakewalk has the following parameter(s):

  • calorie: an integer array that represents calorie count for each cupcake

Input Format

The first line contains an integer , the number of cupcakes in .
The second line contains  space-separated integers .


Output Format

Print a long integer denoting the minimum number of miles Marc must walk to maintain his weight.

Sample Input 0

1 3 2

Sample Output 0


Explanation 0

Let's say the number of miles Marc must walk to maintain his weight is . He can minimize  by eating the  cupcakes in the following order:

  1. Eat the cupcake with  calories, so .
  2. Eat the cupcake with  calories, so .
  3. Eat the cupcake with  calories, so .

We then print the final value of , which is , as our answer.

Sample Input 1

7 4 9 6

Sample Output 1


Explanation 1

Solution in Python

def marcsCakewalk(calorie):
    return sum(calorie[i]*2**i for i in range(len(calorie)))
calorie = sorted(map(int,input().split()), reverse=True)

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.