In this HackerRank Climbing the Leaderboard problem you need to complete the climbingLeaderboard function that has two integer arrays as parameters and then it needs to return the player's rank after each new score.

HackerRank Climbing the Leaderboard problem solution


Problem solution in Python programming.

#!/bin/python3

import sys
from collections import Counter
from bisect import bisect_left

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(' ')]
# your code goes here

counts = Counter(scores)
counts = sorted(counts)
n = len(counts)
for a in alice:
    i = bisect_left(counts, a)
    if i < n and counts[i]==a:
        print(n - i)
    else:
        print(n+1-i)
        


Problem solution in Java Programming.

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();
        int[] scores = new int[n];
        for(int scores_i=0; scores_i < n; scores_i++){
            scores[scores_i] = in.nextInt();
        }
        int m = in.nextInt();
        int[] alice = new int[m];
        for(int alice_i=0; alice_i < m; alice_i++){
            alice[alice_i] = in.nextInt();
        }
        printScores(n, scores, alice);
    }
    
    public static void printScores(int n, int[] scores, int[] alice){
        ArrayList<Integer> places = new ArrayList<Integer>();
        int place = 1;
        places.add(scores[0]);
        for(int i = 1; i < scores.length; i++){
            int currPlace = place-1;
            int currScore = scores[i];
            if(currScore != places.get(currPlace)){
                place++;
                places.add(currScore);
            }
        }
        
        int currPlace = places.size() + 1;
        for(int i = 0; i < alice.length; i++){
            currPlace = getPlace(currPlace, alice[i], places);
            System.out.println(currPlace);
        }
        
    }
    
    public static int getPlace(int currPlace, int currScore, ArrayList<Integer> places){
        for(int i = currPlace - 1; i > 0; i--){
            int index = i - 1;
            if(currScore < places.get(index)){
                return i + 1;
            }
        }
        return 1;
    }
    
    
    
    
}


Problem solution in C++ programming.

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <numeric>
#include <memory.h>
#include <time.h>

using namespace std;

typedef long long LL;


int n, m;

int main()
{
    scanf("%d", &n);
    set<int> s;
    for (int i = 0; i < n; ++i)
    {
        int x;
        scanf("%d", &x);
        s.insert(x);
    }
    scanf("%d", &m);
    vector<int> v(s.begin(), s.end());
    for (int i = 0; i < m; ++i)
    {
        int x;
        scanf("%d", &x);
        int pos = upper_bound(v.begin(), v.end(), x) - v.begin();
        printf("%d\n", (int)v.size() - pos + 1);
    }
    return 0;
}


Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,t,max_rank=0,cr,cs=0; 
    scanf("%d",&n);
    int *scores = malloc(sizeof(int) * n);
    for(int scores_i = 0; scores_i < n; scores_i++)
    {
        scanf("%d",&t);
        if(max_rank)
        {
            if(scores[max_rank-1]==t)
            {
                continue;  
            }
        }
        scores[max_rank++]=t;
    }
    cr=max_rank+1;
    //printf(" %d\n",cr);
    int m; 
    scanf("%d",&m);
    int *alice = malloc(sizeof(int) * m);
    for(int alice_i = 0; alice_i < m; alice_i++){
       scanf("%d",&alice[alice_i]);
    }
    // your code goes here
    int temp;
    for(int i=0;i<m;i++)
    {
        cs=alice[i];
        temp=cr;
        while(temp>0 && scores[--temp]<=cs ) cr=temp;
        printf("%d\n",cr+1);
    }
    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function search(s, a, i, j, r) {
    //console.log(s.slice(i, j));
    if ((j-i) <= 1) {
        //console.log(i, j, s[i], s[j], r[s[i]], r[s[j]]);
        if (a>s[i]) {
            return (r[s[i]] - 1)>0?r[s[i]] - 1:1;
        } else if (a==s[i]) {
            return r[s[i]];
        } else if (a>s[j]) {
            return r[s[i]]+1;
        } else if (a==s[j]) {
            return r[s[j]]==undefined?r[s[j-1]]:r[s[j]];
        } else {
            return r[s[j]]==undefined?r[s[j-1]]+1:r[s[j]]+1;
        }
    }
    var mid = Math.floor(i+(j-i) / 2);
    //console.log(i, j, mid);
    if (scores[mid] < a) {
        return search(s, a, i, mid, r);
    } else {
        return search(s, a, mid, j, r);
    } 
}

function main() {
    var n = parseInt(readLine());
    scores = readLine().split(' ');
    scores = scores.map(Number);
    var m = parseInt(readLine());
    alice = readLine().split(' ');
    alice = alice.map(Number);
    // build a map of ranks
    var ranks = new Array();
    var rank = 1;
    ranks[scores[0]] = rank;
    for(var j = 1; j < n; j++) {
        if (scores[j] != scores[j-1]) {
            rank++;
            ranks[scores[j]] = rank;
        }
    }
    //console.log(ranks);
    // binary search
    for(var j = 0; j < m; j++) {
        console.log(search(scores, alice[j], 0, scores.length, ranks));
    }
}