HackerRank leaderboard challenge I have been doing this challenge in which I need to determine and return one's (i.e. alice) rank on a leaderboard by comparing her scores to the others'. I made this work in all but this one case where there are 200000 scores on the board, so I believe my program is not effective at large scale. I am receiving a timeout error. I tried to shorten the program as much as possible but I think loops are still too much of a problem. I would appreciate any suggestions.

#include &lt;bits/stdc++.h&gt;

using namespace std;

vector &lt;int&gt; climbingLeaderboard(vector &lt;int&gt; scores, vector &lt;int&gt; alice) {
    vector&lt;int&gt; result;
    reverse(alice.begin(), alice.end());
    int lastIndex = 0;
    int lastRank = 1;

    for (int i = 0; i &lt; alice.size(); i++) {

        int rank = lastRank;
        for (int j = lastIndex; j &lt; scores.size(); j++) {
            if (alice[i] &gt;= scores[j]) {
                lastIndex = j;
                break;
            }

            else if (alice[i] &lt; scores[j]) {
                rank++;
            }

            if (scores[j] == scores[j+1] &amp;&amp; rank != 1) {
                rank--;
            }
        }
        result.push_back(rank);   
        lastRank = rank;
    }
    reverse(result.begin(), result.end());

    return result;
}

int main() {
    int n;
    cin &gt;&gt; n;
    vector&lt;int&gt; scores(n);
    for(int scores_i = 0; scores_i &lt; n; scores_i++){
       cin &gt;&gt; scores[scores_i];
    }
    int m;
    cin &gt;&gt; m;
    vector&lt;int&gt; alice(m);
    for(int alice_i = 0; alice_i &lt; m; alice_i++){
       cin &gt;&gt; alice[alice_i];
    }
    vector &lt;int&gt; result = climbingLeaderboard(scores, alice);
    for (ssize_t i = 0; i &lt; result.size(); i++) {
        cout &lt;&lt; result[i] &lt;&lt; (i != result.size() - 1 ? "\n" : "");
    }
    cout &lt;&lt; endl;


    return 0;
}


Sample Input

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


Sample Output

6
4
2
1


Explanation

The second row of input is composed of the scores on the leaderboard and the fourth row contains the scores of Alice. The score she gets during each attempt is compared with other scores on the leaderboard to determine her rank. Dense Ranking is used here. So from the sample input, the first two 100s on the leaderboard rank 1st and then 50 ranks the 2nd, both 40s rank 3rd and so on.
don't use using namespace std

You should not use using namespace std because it eliminates some important information, which may lead to some conflicts with your classes or classes of other libraries. It is not uncommon to have a vector class, which has a totally other meaning than the std::vector. And to be honest, it isn't that much more work, to type std:: in front of the classes/functions, is it?

use std algorithms

You do everything with for loops, which is bloated and error prone. Use the algorithms of the std namespace instead. I provide you with a short an clean example, how you could do the above task (and more) with less lines.

your algorithm itself

You should think about logical improvements. Is it necessary to store every score in a vector, when equal scores produce the same rankings, and the following scores just increments by one? Therefore I took the std::unique algorithm, which returns a forward iterator. std::unique doesn't erase anything itself. It just moves duplicates to the end of the range (our std::vector); you have to erase them manually.
But, before you can use std::unique you have to order your elements. I don't know if it's guaranteed to get the score input in a descending order; thus I simply use std::sort to ensure this.
After that, it's a simple std::lower_bound (which is a binary search instead a linear search) to get the first iterator to an element, which is not less than the provided score (ok, I had to pass an other function object, because we want to check for greater and not for less).
Iterators starts with a zero index. This means, we have to add 1 to our index, to get the official ranking.
Easy, huh?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;

int main()
{
    std::vector&lt;int&gt; scores { 100, 100, 50, 40, 40, 20, 10 };
    std::vector&lt;int&gt; aliceScores { 5, 25, 50, 120 };
    // sort and erase duplicates
    std::sort(std::begin(scores), std::end(scores), std::greater&lt;int&gt;());
    scores.erase(std::unique(std::begin(scores), std::end(scores)), std::end(scores));

    // just a lambda to make the output part more readable
    auto locateRanking = [](const auto&amp; _scores, int _newScore){
        auto itr = std::lower_bound(std::begin(_scores), std::end(_scores), _newScore, std::greater&lt;int&gt;());
        return std::distance(std::begin(_scores), itr) + 1;
    };

    // output part
    for (auto newScore : aliceScores)
    {
        std::cout &lt;&lt; locateRanking(scores, newScore) &lt;&lt; std::endl;
    }
}


tl;dr

Sort the vector, remove the duplicates and use a binary search instead of your linear approach and you will discover a huge performance benefit.