Code for calculating the books dispatched Recently I came across this, here is an excerpt from it,


  This is another problem about Indraneel's library. His library has one
  long shelf. His books are numbered and he identifies the books by
  their number. Each book has a distinct number.
  
  He has lost many books, since many of his friends borrow his books and
  never bother to return them. He does not want to lose any more books
  and has decided to keep a record of all books that he lends to his
  friends. To make the task of borrowing a book a little difficult, he
  has given the following instructions to his friends: when they borrow
  a book, they must record in a register its position from the left
  among the books currently on the shelf.
  
  Suppose there are 5 books in the library and they are arranged as
  follows:
  
  26  1  42  15  3
  
  If someone walks in and borrows the book 42, then he will record 3 in
  the register because this book is the third from the left on the
  shelf. Now the shelf looks like this:
  
  26  1  15  3
  
  If the next person borrow the book 3, he writes down 4 in the register
  since this is currently the fourth book from the left on the shelf,
  and so on.
  
  Indraneel knows the initial arrangement of the books in his library at
  the time that he introduced the register system. After a while he
  examines his register and would like to know which books have been
  borrowed. Your task is to write a program to help Indraneel solve this
  problem.


My approach is count from left if the value of the book is -1 then skip the the book , when the count is equal to the count of the book then print the name of the book and make that value -1.But this approach though correct is giving time limit exceeded on a handful of testcases. Anyone having a better approach for this problem?

Here is the code:

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

int main(){
    //Input for the number books with the values
    int m;
    std::cin &gt;&gt; m;
    std::vector&lt;int&gt;books(m);

    for(int i=0;i&lt;m;i++){
        std::cin&gt;&gt;books[i];
    }
    //Input for the dispatches made
    int n;
    std::cin &gt;&gt; n;
    std::vector&lt;int&gt;taken(n);

    for(int i=0;i&lt;n;i++){
        std::cin &gt;&gt; taken[i];
    }

    for(int i=0;i&lt;n;i++){
        int bookNum = taken[i]-1;                       //Get the position of book needed
        int k=0;
        for(int j=0;j&lt;m;j++){                          //Go through the book array
            if(books[j] != -1){
                if(k == bookNum){
                    std::cout &lt;&lt; books[j] &lt;&lt; std::endl; //Display the name if the k is equal
                    books[j] = -1;
                    break;
                }
                k++;                                    //Increment k if the value of the book is not negative
            }
        }
    }

    return 0;
}


Here are the testcases.
There are some minor nits about the general code


Separate operators like &gt;&gt; with spaces, that makes the code much easier to read. Also you should separate language constructs like for or while with a space afterwards, to readily separate them from function calls.
You should use range based loops when you already have the vector fully initialized, it is much more concise. 

int numBooks;
std::cin &gt;&gt; numBooks;
std::vector&lt;int&gt; books(numBooks);
for (auto &amp;book : books){
    std::cin &gt;&gt; book;
}

The return 0; at the end is not necessary anymore with modern compilers.
You should prefer pre increment ++book over post increment book++, as the later involves a copy (at least for non trivial data types)
Actually your code itself is way to complicated. Rather than assigning a new vector you can just shrink the old one

int numTaken;
std::cin &gt;&gt; numTaken;
for (int book = 0; book &lt; numTaken; ++book) {
    int bookId;
    std::cin &gt;&gt; bookId;
    std::cout &lt;&lt; books[bookId];
    books.erase(bookId);
}



Full code

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

int main() {
    std::ios_base::sync_with_stdio(false);
    size_t numBooks;
    std::cin &gt;&gt; numBooks;
    std::vector&lt;int&gt; books;
    books.reserve(numBooks);
    for (size_t book = 0; book &lt; numBooks; ++book) {
        size_t bookId;
        std::cin &gt;&gt; bookId;
        books.push_back(bookId);
    }

    size_t numBorrowed;
    std::cin &gt;&gt; numBorrowed;
    for (size_t borrow = 0; borrow &lt; numBorrowed; ++borrow) {
        size_t bookId;
        std::cin &gt;&gt; bookId;
        std::cout &lt;&lt; books[bookId] &lt;&lt; "\n";
        books.erase(bookId);
    }
}