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 Problem – Marc’s Cakewalk – Hacker Rank Solution
Marc's Cakewalk – Hacker Rank Solution
Problem:
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.
Example
![Marc's Cakewalk in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 101](https://technorj.com/wp-content/uploads/2021/12/image-101.png)
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, .
Constraints
![Marc's Cakewalk in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 image 103](https://technorj.com/wp-content/uploads/2021/12/image-103.png)
Sample Input 0
3 1 3 2
Sample Output 0
11
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:
- Eat the cupcake with calories, so .
- Eat the cupcake with calories, so .
- Eat the cupcake with calories, so .
We then print the final value of , which is , as our answer.
Sample Input 1
4 7 4 9 6
Sample Output 1
79
Explanation 1
![Marc's Cakewalk in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 4 image 102](https://technorj.com/wp-content/uploads/2021/12/image-102.png)
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 break; } } return a; } 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(); } 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++)); } System.out.println(sum); in.close(); } }