Marc’s Cakewalk in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Marc’s Cakewalk in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. 

Advantages of Algorithms:

  • It is easy to understand.
  • Algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemMarc’s Cakewalk – Hacker Rank Solution

Marc's Cakewalk – Hacker Rank 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.


image 101

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


  • 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, .


image 103

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

image 102
Marc's Cakewalk – Hacker Rank Solution
import java.util.Scanner;

 * @author Techno-RJ
public class MarcsCakewalk {
	public static int[] sort(int a[]) {

		for (int i = 1; i < a.length; i++) {
			for (int j = i; j > 0; j--) {
				if (a[j] < a[j - 1]) {
					int temp = a[j - 1];
					a[j - 1] = a[j];
					a[j] = temp;
				} else
		return a;

	public static void main(String[] args) {
		Scanner in = new Scanner(;
		int n = in.nextInt();
		int[] calories = new int[n];
		for (int calories_i = 0; calories_i < n; calories_i++) {
			calories[calories_i] = in.nextInt();
		int sortedArray[] = sort(calories);
		int j = 0;
		long sum = 0;
		for (int i = sortedArray.length - 1; i >= 0; i--) {
			sum = (sum + sortedArray[i] * (long) Math.pow(2, j++));

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock