# Climbing the leaderboard challenge

**Problem statement**

Alice is playing an arcade game and wants to climb to the top of the leaderboard. Can you help her track her ranking as she beats each level? 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.

For example, four players have the scores , , , and . Those players will have ranks , , , and , respectively.

When Alice starts playing, there are already people on the leaderboard. The score of each player is denoted by . Alice plays for levels, and we denote her total score after passing each level as . After completing each level, Alice wants to know her current rank.

You are given an array, , of decreasing leaderboard scores, and another array, , of Alice’s cumulative scores for each level of the game. You must print integers. The integer should indicate the current rank of alice after passing the level.

**Input Format**

The first line contains an integer, , denoting the number of players already on the leaderboard.

The next line contains space-separated integers describing the respective values of .

The next line contains an integer, , denoting the number of levels Alice beats.

The last line contains space-separated integers describing the respective values of .

**Constraints**

The existing leaderboard, , is in descending order.

Alice’s scores are cumulative, so is in ascending order.

**Output Format**

Print integers. The integer should indicate the rank of alice after passing the level.

**Sample Input 0**

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

**Sample Output 0**

6 4 2 1

**Explanation 0**

Alice starts playing with players already on the leaderboard, which looks like this:

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

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

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

After Alice finishes level , her score is and her ranking is 1:

**Solution**

The first step we are going to take is determine the largest possible rank. Note that same scores are ranked equally.

int maxRank = 1; for (int i = 1; i < n; i++) { if (score[i] != score[i - 1]) { maxRank++; } }

Once we have our largest possible rank, we place Alice in the last position. That is, Alice starts at rank . Since the are already sorted in descending order we can start iterating over them in reverse order starting from the last one while keeping an index of the current score named . We will then keep decreasing Alice's rank (move her one position up in the leaderboard) while her score is greater than the current score. We also keep decreasing our while we have equal consecutive scores. In case of consecutive equal scores we must make sure we decrease Alice's rank only once.

The full code is listed below.

**Full code**

public class ClimbingTheLeaderboard { public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream(System.getProperty("user.home") + "/" + "in.txt")); Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] score = new int[n]; for (int i = 0; i < n; i++) { score[i] = in.nextInt(); } int m = in.nextInt(); int[] alice = new int[m]; for (int i = 0; i < m; i++) { alice[i] = in.nextInt(); } int maxRank = 1; for (int i = 1; i < n; i++) { if (score[i] != score[i - 1]) { maxRank++; } } int rank = maxRank + 1; int cursor = n - 1; for (int a = 0; a < m; a++) { while (cursor >= 0 && alice[a] >= score[cursor]) { boolean flag = false; do { cursor--; if (!flag) { rank = Math.max(1, rank - 1); flag = true; } } while (cursor > 0 && score[cursor + 1] == score[cursor]); } System.out.println(rank); } } }