Climbing the Leaderboard 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 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 ProblemClimbing 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.
image 74

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

image 75

Sample Input 1CopyDownload

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:

image

After Alice finishes game , her score is  and her ranking is :

image

After Alice finishes game , her score is  and her ranking is :

image

After Alice finishes game , her score is  and her ranking is tied with Caroline at :

image

After Alice finishes game , her score is  and her ranking is :

image

Sample Input 2CopyDownload

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;
	}

}

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