# Climbing the Leaderboard | HackerRank Solution

Hello coders, today we are going to solve Climbing the Leaderboard HackerRank Solution which is a Part of HackerRank Algorithm Series.

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Â 1Â on the leaderboard.
• Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

Example

ranked = [100, 90, 90, 80]
player = [70, 80, 105]
The ranked players will have ranksÂ 1,Â 2,Â 2, andÂ 3, respectively. If the player’s scores areÂ 70,Â 80Â andÂ 105, their rankings after each game areÂ 4th,Â 3rdÂ andÂ 1st. ReturnÂ [4, 3, 1].

Function Description

Complete the climbingLeaderboard function in the editor below.

• 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

The first line contains an integerÂ n, the number of players on the leaderboard.
The next line containsÂ nÂ space-separated integersÂ ranked[i], the leaderboard scores in decreasing order.
The next line contains an integer,Â m, the number games the player plays.
The last line containsÂ mÂ space-separated integersÂ player[j], the game scores.

## Constraints

• Â 1 <= n <= 2 x 105
• 1 <= m <= 2 x 105
• 0 <= ranked[i] <= 109 for 0 <= i < nÂ
• 0 <= player[j] <= 109Â forÂ 0 <= j < m
• The existing leaderboard,Â ranked, is inÂ descendingÂ order.
• The player’s scores,Â player, are inÂ ascendingÂ order.

ForÂ 60%Â of the maximum score:

• 1 <= n <= 200
• 1 <= m <= 200

Sample Input 1

``````7
100 100 50 40 40 20 10
4
5 25 50 120``````

Sample Output 1

``````6
4
2
1``````

Explanation 1

Alice starts playing withÂ 7Â players already on the leaderboard, which looks like this:
After Alice finishes gameÂ 0, her score isÂ 5Â and her ranking isÂ 6:
After Alice finishes gameÂ 1, her score isÂ 25Â and her ranking isÂ 4:
After Alice finishes gameÂ 2, her score isÂ 50Â and her ranking is tied with Caroline atÂ 2:
After Alice finishes gameÂ 3, her score isÂ 120Â and her ranking isÂ 1:

Sample Input 2

``````6
100 90 90 80 75 60
5
50 65 77 90 102``````

Sample Output 2

``````6
5
4
2
1``````

## Solution – Climbing the Leaderboard

### C++

```#include <ios>
#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> vec;

int main()
{
int n, q, v;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> v;
vec.push_back(v);
}
std::sort(vec.begin(), vec.end());
vec.resize(std::distance(vec.begin(), std::unique(vec.begin(), vec.end())));
std::cin >> q;
while (q--)
{
std::cin >> v;
std::cout << std::distance(std::upper_bound(vec.begin(), vec.end(), v), vec.end())+1 << '\n';
}
}```

### Python

```import sys
import bisect

n = int(input().strip())
scores = [int(scores_temp) for scores_temp in input().strip().split(' ')]
m = int(input().strip())
alice = [int(alice_temp) for alice_temp in input().strip().split(' ')]

scores = list(sorted(set(scores)))
aa = [bisect.bisect_right(scores, a) for a in alice]
for a in aa:
v = len(scores) + 1 - a
print(v)```

### Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Stack<Integer> scores = new Stack<>();
scores.push(in.nextInt());
for (int i = 1; i < n; i++) {
int cur = in.nextInt();
if (!scores.peek().equals(cur)) scores.push(cur);
}
int m = in.nextInt();
for (int i = 0; i < m; i++) {
int cur = in.nextInt();
while (scores.size() > 0 && cur > scores.peek()) scores.pop();
System.out.println(scores.size() +(scores.size() > 0 && scores.peek().equals(cur) ? 0 : 1));
}
}
}
```

Disclaimer: The above Problem (Climbing the Leaderboard) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.