C++ 0ms Explained Easy one pass
Three cases possible are,\n\nIf the ticket holders have less tickets to buy than our Kth ticket holder, they will have got all the tickets they wanted before kt
Three cases possible are,\n\n`If` the ticket holders have less tickets to buy than our Kth ticket holder, they will have got all the tickets they wanted before kth ticket holder gets all its tickets,\n\n`else if` the ticket holders have more ticketsto buy than our Kth ticket holder, and they are ahead in line(position < k) then, they will have bought `tickets[k]` tickets before kth ticket holder gets all the tickets it needs,\n\n`else if` the ticket holders have more tickets to buy than our Kth ticket holder, and they are behind in line(position > k) then, they will have bought 1 ticket less than `tickets[k]` before kth ticket holder gets all the tickets it needs.\n\n```\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        \n        int ticket = tickets[k];\n        int res = 0;\n        for(int i = 0; i<tickets.size(); i++){\n          if(tickets[i] < ticket) res+=tickets[i];\n          else if(tickets[i] >= ticket and i <= k) res+=ticket;\n          else if(tickets[i] >= ticket and i > k) res+=ticket-1;\n        }\n        return res;\n    }\n};\n```
['C', 'C++']