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 Climbing the Leaderboard 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 – Climbing the Leaderboard – Hacker Rank Solution
Climbing the Leaderboard – Hacker Rank Solution
Problem:
An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this:
- The player with the highest score is ranked number on the leaderboard.
- Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 74](https://technorj.com/wp-content/uploads/2021/12/image-74.png)
Function Description
Complete the climbingLeaderboard function in the editor below.
climbingLeaderboard has the following parameter(s):
- int ranked[n]: the leaderboard scores
- int player[m]: the player’s scores
Returns
- int[m]: the player’s rank after each new score
Input Format
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 image 75](https://technorj.com/wp-content/uploads/2021/12/image-75.png)
Array: ranked1001005040402010 Array: player52550120
7
100 100 50 40 40 20 10
4
5 25 50 120
Sample Output 1
6421
Explanation 1
Alice starts playing with players already on the leaderboard, which looks like this:
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 4 image](https://s3.amazonaws.com/hr-challenge-images/0/1481263702-9b5e9abd56-climbingrank.png)
After Alice finishes game , her score is and her ranking is :
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 5 image](https://s3.amazonaws.com/hr-challenge-images/0/1481263847-2443e11cea-climbingrank1.png)
After Alice finishes game , her score is and her ranking is :
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 6 image](https://s3.amazonaws.com/hr-challenge-images/0/1481264155-cb76495070-climbingrank3.png)
After Alice finishes game , her score is and her ranking is tied with Caroline at :
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 7 image](https://s3.amazonaws.com/hr-challenge-images/0/1481264229-a216b3a974-climbingrank4.png)
After Alice finishes game , her score is and her ranking is :
![Climbing the Leaderboard in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 8 image](https://s3.amazonaws.com/hr-challenge-images/0/1481264323-30f93fa8de-climbingrank5.png)
Array: ranked1009090807560 Array: player50657790102
6
100 90 90 80 75 60
5
50 65 77 90 102
Sample Output 2
65421
Climbing the Leaderboard – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class ClimbingTheLeaderboard { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] scores = new int[n]; scores[0] = in.nextInt(); int k = 1, counter = 0; for (int scores_i = 1; scores_i < n; scores_i++) { int temp = in.nextInt(); if (temp != scores[k - 1]) { scores[k++] = temp; } else { counter++; } } for (int i = scores.length - 1; i >= 0 && counter > 0; i--) { counter--; scores[i] = Integer.MIN_VALUE; } int m = in.nextInt(); for (int alice_i = 0; alice_i < m; alice_i++) { int tmp = in.nextInt(); if (tmp > scores[0]) { System.out.println(1); } else if (tmp < scores[scores.length - 1]) { System.out.println(scores.length + 1); } else { System.out.println(binarySearch(scores, tmp) + 1); } } in.close(); } private static int binarySearch(int[] a, int key) { int lo = 0; int hi = a.length - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (a[mid] == key) { return mid; } else if (a[mid] < key && key < a[mid - 1]) { return mid; } else if (a[mid] > key && key >= a[mid + 1]) { return mid + 1; } else if (a[mid] < key) { hi = mid - 1; } else if (a[mid] > key) { lo = mid + 1; } } return -1; } }