Finding the horse number you will get

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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 <= T <= 100, 1 <= N <= 1000, 1 <= K <= 10^9, 1 <= M(i) <= 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<iostream>
using namespace std;

int main()

int t;
cin>>t;
while(t--)
int n,k;
cin>>n>>k;
int hrs[n], ans;
for(int i = 0;i < n;i++)
cin>>hrs[i];

if(n >= k)
cout<<k<<endl;
else
int tt = 0,p = n;
int B[n];
for(int j = 0;j < n;j++)
B[j] = hrs[j];

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





cout<<ans+1<<endl;




return 0;







share|improve this question



























    up vote
    1
    down vote

    favorite












    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 <= T <= 100, 1 <= N <= 1000, 1 <= K <= 10^9, 1 <= M(i) <= 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<iostream>
    using namespace std;

    int main()

    int t;
    cin>>t;
    while(t--)
    int n,k;
    cin>>n>>k;
    int hrs[n], ans;
    for(int i = 0;i < n;i++)
    cin>>hrs[i];

    if(n >= k)
    cout<<k<<endl;
    else
    int tt = 0,p = n;
    int B[n];
    for(int j = 0;j < n;j++)
    B[j] = hrs[j];

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





    cout<<ans+1<<endl;




    return 0;







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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 <= T <= 100, 1 <= N <= 1000, 1 <= K <= 10^9, 1 <= M(i) <= 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<iostream>
      using namespace std;

      int main()

      int t;
      cin>>t;
      while(t--)
      int n,k;
      cin>>n>>k;
      int hrs[n], ans;
      for(int i = 0;i < n;i++)
      cin>>hrs[i];

      if(n >= k)
      cout<<k<<endl;
      else
      int tt = 0,p = n;
      int B[n];
      for(int j = 0;j < n;j++)
      B[j] = hrs[j];

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





      cout<<ans+1<<endl;




      return 0;







      share|improve this question













      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 <= T <= 100, 1 <= N <= 1000, 1 <= K <= 10^9, 1 <= M(i) <= 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<iostream>
      using namespace std;

      int main()

      int t;
      cin>>t;
      while(t--)
      int n,k;
      cin>>n>>k;
      int hrs[n], ans;
      for(int i = 0;i < n;i++)
      cin>>hrs[i];

      if(n >= k)
      cout<<k<<endl;
      else
      int tt = 0,p = n;
      int B[n];
      for(int j = 0;j < n;j++)
      B[j] = hrs[j];

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





      cout<<ans+1<<endl;




      return 0;









      share|improve this question












      share|improve this question




      share|improve this question








      edited Aug 2 at 15:53
























      asked Aug 2 at 14:01









      vaibnak

      285




      285




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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 >> n >> 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.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200817%2ffinding-the-horse-number-you-will-get%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            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 >> n >> 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.






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              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 >> n >> 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.






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                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 >> n >> 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.






                share|improve this answer













                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 >> n >> 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.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Aug 2 at 16:10









                Toby Speight

                17.1k13485




                17.1k13485






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200817%2ffinding-the-horse-number-you-will-get%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation