✅Easy✨||C++|| Beats 100% || With Explanation ||
\n# Approach\n Describe your approach to solving the problem. \n1. To solve this problem, we\'ll maintain a count variable to track the time until the Kth perso
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. To solve this problem, we\'ll maintain a count variable to track the time until the Kth person buys all available tickets. We\'ll also use an index variable to iterate through the tickets array.\n\n2. We\'ll iterate through the tickets array until the value of the ticket at the Kth position becomes zero, indicating that all tickets have been purchased. Within the loop, we\'ll check two conditions to determine whether to increment the count. If the value of the ticket at the current index is greater than zero, we\'ll decrement the ticket value, increase the count, and increment the index by 1. Additionally, we\'ll use modulo operation to ensure that the index doesn\'t go out of bounds. If the ticket value is zero at the current index, we\'ll simply increment the index by 1.\n\n3. Once the loop terminates, we\'ll return the count value, representing the time taken for the Kth person to buy all available tickets.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int count = 0; // Variable to store the time\n        int index = 0; // Variable to iterate through the array\n        \n        // Run the loop until the value at the k index becomes zero\n        while (tickets[k] != 0) {\n            if (tickets[index] > 0) {\n                count++;\n                tickets[index]--;\n                index = (index + 1) % tickets.size();\n            } else if (tickets[index] == 0) {\n                index = (index + 1) % tickets.size();\n            }\n        }\n        \n        return count;\n    }\n};\n```
['C++']