Using queue
Approach\n1.We have to return the time when the kth index of tickets are completely sold.\n2.So, from this it is clear that we should have the info about the in
# Approach\n1.We have to return the time when the kth index of tickets are completely sold.\n2.So, from this it is clear that we should have the info about the index also. So, we will be using pair<int, int>.\n3.Run a loop and store in queue {tickets[i], i}.\n4.Run a loop until queue is empty and pick the ticket, index and remove   them.\n    since tickets are initially > 0, so ticket - 1 and ans + 1.\n    if(ticket > 0) push it to queue.\n    now if ticket is completely sold and and the index == k, that is the  time we need to return. So break it\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: I have used queue so S.C. -> O(N)\n\n# Code\n```\nclass Solution {\npublic:\n    // Since we need to find the time taken to buy the tickets of specific index\n    // we should have info about index also so that whenever the kth index tickets are completely\n    // that is the time we need to return\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n\n        queue<pair<int, int>> q;\n        for(int i = 0; i < tickets.size(); i++){\n            q.push({tickets[i], i});\n        }\n        int ans = 0;\n        while(!q.empty()){\n            // take out the index and tickets\n            int ticket = q.front().first;\n            int ind = q.front().second;\n            // now remove it\n            q.pop();\n\n            ticket--;\n            ans++;\n            // if there are remaining tickets\n            if(ticket > 0) q.push({ticket, ind});\n            // when the given index tickets are completely sold then break the loop\n            if(ind == k && ticket == 0) break;\n        }\n        return ans;\n    }\n};\n```
['C++']