Finding the horse number you will get I got this question from the interviewbit site test.

There are n horses and you are the kth person in the line to ride on the horse. All the persons in the queue will go with the lowest numbered horse available i.e if horse no.1 and horse no.2 are present , the person will choose horse no.1 .All the horses have their own riding time and after their riding time ends they are free for next round. If there are no horses available the person will wait for the horse to come and will go on with it. We have to find that on which horse number we will be riding.

Input:- T - no. of test cases, n - no. of horses, k - your number in queue,ride time(M) - n numbers which will denote the riding time of the horses

constraints:- 1 &lt;= T &lt;= 100, 1 &lt;= N &lt;= 1000, 1 &lt;= K &lt;= 10^9, 1 &lt;= M(i) &lt;= 100000

output :- you have to tell the horse number which you will get.

Ex. 1

3 8

4 2 1

output:- 
1

Here there are 3 horses with running time 4, 2, 1 and i am 8th in the queue.
so first three persons will go with horse#1 , horse#2 and horse#3. After this 
there will be no horse so the fourth person will have to wait. horse#3 will 
come first so he will go with it. fifth will go with horse#2 and sixth with 
horse#3, seventh will go with horse#3 and eight will go with horse #1. so the 
output is 1.
Below is my code :-

Its time complexity is O(N*K). Is there a better way to solve it. 

#include&lt;iostream&gt;
using namespace std;

int main(){

    int t;
    cin&gt;&gt;t;
    while(t--){
        int n,k;
        cin&gt;&gt;n&gt;&gt;k;
        int hrs[n], ans;
        for(int i = 0;i &lt; n;i++)
        cin&gt;&gt;hrs[i];

        if(n &gt;= k){
            cout&lt;&lt;k&lt;&lt;endl;
        }else{
            int tt = 0,p = n;
            int B[n];
            for(int j = 0;j &lt; n;j++)
            B[j] = hrs[j];

            while(p != k){
                tt++;
                for(int m = 0;m &lt; n;m++){
                    if(tt == hrs[m]){
                        p++;
                        hrs[m] += B[m];
                        if(p == k){
                         ans = m;
                         break;
                        }
                    }
                }

            }
            cout&lt;&lt;ans+1&lt;&lt;endl;
        }

      }

    return 0;
}
Use portable constructs

Variable-length arrays are not standard C++.  Using such non-standard extensions hampers your code because you can't easily use different compilers (this can possibly even exclude entire target platforms).

Avoid importing all of std into the global namespace

Namespaces provide us with an important service, separating the large and growing set of identifiers in std from those of our own code.  It's actively harmful to reverse that benefit with using namespace std.

Always test for errors when reading

Formatted input such as std::cin &gt;&gt; n &gt;&gt; k should always test the state of the stream before using the read values.

Use clearer names

We don't have to use names like t just because that's what the question uses to describe the number of test cases.  Similarly, hrs took me much longer to comprehend than would horse - and that's not going to push your line lengths.

Know your standard library

What we have is a priority queue of horses, ordered by the time they next return, with the horse's number as tie-breaker.  We have std::priority_queue with insertion/removal times that scale as O(log n), so iterating over the k previous riders to find which horse we get will scale as O(k log n).

The algorithm is pretty simple - for each member of the queue in front of us, take the next horse from the stable (a std::priority_queue), add its running time to its next-available time, and insert it back into the stable.  The horse at the front of the stable after k-1 iterations will be ours.