Marc's Cakewalk


Problem Statement :


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.



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.

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 , the number of cupcakes in .
The second line contains  space-separated integers, .



Solution :



title-img


                            Solution in C :

In  C  :





#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>


void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b=temp;
}



int partition(int *x,int start,int end)
{
    int pivot,pindex,i;
    pivot = x[end];
    pindex = start;
    for(i=start;i<end;i++)
    {
        if(x[i]>=pivot)
          {
            swap(&x[i],&x[pindex]);
            pindex = pindex + 1;
          }
    }
    swap(&x[pindex],&x[end]);
    return pindex;
}
void quicksort(int *x,int start,int end)
{

    if(start<end)
   {
     int i = partition(x,start,end);
      quicksort(x,start,i-1);
      quicksort(x,i+1,end);


    }
}

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]);
    }
    quicksort(calories,0,n-1);
    int i;
    long long int sum = 0;
    for(i=0;i<n;i++)
    {
        sum += calories[i]*((long long int)pow(2,i));
    }    
    printf("%lld",sum);
    // your code goes here
    return 0;
}
                        


                        Solution in C++ :

In  C ++  :






#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; }

int main() {
	int n;
	while (~scanf("%d", &n)) {
		vector<int> c(n);
		for (int i = 0; i < n; ++ i)
			scanf("%d", &c[i]);
		sort(c.begin(), c.end());
		ll ans = 0;
		rep(i, n)
			ans += (ll)c[i] << (n - 1 - i);
		printf("%lld\n", ans);
	}
	return 0;
}
                    


                        Solution in Java :

In  Java :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] calories = new int[n];
        for(int calories_i=0; calories_i < n; calories_i++){
            calories[calories_i] = in.nextInt();
        }
        Arrays.sort(calories);
        long ans = 0;
        for (int i = 0; i < n; i++) {
            ans += ((long)calories[n-i-1])<<i;
        }
        System.out.println(ans);
    }
}
                    


                        Solution in Python : 
                            
In  Python3 :






#!/bin/python3

import sys


n = int(input().strip())
calories = list(map(int, input().strip().split(' ')))
calories.sort()

toplam = 0
count = 0
for kalori in calories[::-1]:
    toplam += kalori * (2 ** count)
    count += 1
print(toplam)
                    


View More Similar Problems

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →