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 Luck Balance 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 – Luck Balance – Hacker Rank Solution
Luck Balance – Hacker Rank Solution
Problem:
Lena is preparing for an important coding competition that is preceded by a number of sequential preliminary contests. Initially, her luck balance is 0. She believes in “saving luck”, and wants to check her theory. Each contest is described by two integers, and :
- is the amount of luck associated with a contest. If Lena wins the contest, her luck balance will decrease by ; if she loses it, her luck balance will increase by .
- denotes the contest’s importance rating. It’s equal to if the contest is important, and it’s equal to if it’s unimportant.
If Lena loses no more than important contests, what is the maximum amount of luck she can have after competing in all the preliminary contests? This value may be negative.
Example
Contest L[i] T[i] 1 5 1 2 1 1 3 4 0
If Lena loses all of the contests, her will be . Since she is allowed to lose important contests, and there are only important contests, she can lose all three contests to maximize her luck at .
If , she has to win at least of the important contests. She would choose to win the lowest value important contest worth . Her final luck will be .
Function Description
Complete the luckBalance function in the editor below.
luckBalance has the following parameter(s):
- int k: the number of important contests Lena can lose
- int contests[n][2]: a 2D array of integers where each contains two integers that represent the luck balance and importance of the contest
Returns
- int: the maximum luck balance achievable
Input Format
The first line contains two space-separated integers and , the number of preliminary contests and the maximum number of important contests Lena can lose.
Each of the next lines contains two space-separated integers, and , the contest’s luck balance and its importance rating.
Constraints
Sample Input
STDIN Function ----- -------- 6 3 n = 6, k = 3 5 1 contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]] 2 1 1 1 8 1 10 0 5 0
Sample Output
29
Explanation
There are contests. Of these contests, are important and she cannot lose more than of them. Lena maximizes her luck if she wins the important contest (where ) and loses all of the other five contests for a total luck balance of .
Luck Balance– Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class LuckBalance { public static void sort(int a[], int lo, int hi) { if (hi > lo) { int q = partition(a, lo, hi); sort(a, lo, q - 1); sort(a, q + 1, hi); } } public static int partition(int a[], int lo, int hi) { int i = lo; int j = hi + 1; while (hi > lo) { while (a[++i] < a[lo]) if (i == hi) break; while (a[--j] > a[lo]) if (j == lo) break; if (i >= j) break; swap(a, i, j); } swap(a, lo, j); return j; } public static void swap(int a[], int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int win = 0; int a[] = new int[N]; int sum = 0; for (int i = 0; i < N; i++) { int temp = sc.nextInt(); if (sc.nextInt() == 1) { win++; a[i] = temp; } else { a[i] = Integer.MAX_VALUE; } sum += temp; } sort(a, 0, a.length - 1); int s2 = 0; for (int i = 0; i < win - K; i++) { s2 += a[i]; } System.out.println(sum - 2 * s2); sc.close(); } }
I’m gone to convey my little brother, that he should also
pay a quick visit this webpage on regular basis to obtain updated from latest news.