✅ Easy Solution in Java | | With explanation🔥
Intuition\n Describe your first thoughts on how to solve this problem. \n- The code aims to find out how much time it would take for a person to buy a certain n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The code aims to find out how much time it would take for a person to buy a certain number of tickets (specified by k) given an array tickets where each element represents the number of available tickets at a particular counter.\n- The code iterates through the array in a circular manner, simulating a round-robin approach.\n- At each iteration, it tries to buy tickets from each counter (tickets[i]), decrementing the count of available tickets and counting the total time (c) taken to buy all the required tickets (k).\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialization:\n\n   - Initialize variables c and i to 0.\n   - c will track the total time taken to buy all tickets, and i will be used to iterate through the array of counters.\n2. Iterative Process:\n\n    - The code enters a while loop which continues until there are no more tickets left at the specified counter k.\n    - Inside the loop, it checks if there are still tickets available at the current counter tickets[k] > 0.\n   - It then iterates through each counter using the variable i:\n     - If there are tickets available at the current counter tickets[i] > 0, it decrements the count of available tickets and increments the total time c.\n     - It then moves to the next counter by incrementing i.\n     - If i reaches the end of the array, it resets i to 0 to continue the circular iteration.\n3. Returning the Result:\n\n     - Once the while loop exits (indicating no more tickets available at counter k), it returns the total time c taken to buy all the tickets.\n\n# Complexity\n- Time complexity:\n  - In the worst-case scenario, the code might iterate through all the counters to buy all the tickets.\n   - Let m be the average number of counters needed to buy all the tickets. The time complexity would be O(m).\n\n- Space complexity:\n  - The space complexity is O(1) as no additional space is used that grows with the size of the input.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int c=0 , i=0;\n        int n= tickets.length;\n        while(tickets[k]>0){\n            if(tickets[i]>0){\n                tickets[i]--;\n                c++;\n            }\n            i++;\n            //System.out.println(c);\n            if(i==n){\n                i=0;\n            }\n        }\n        return c;\n    }\n}\n```
['Array', 'Queue', 'Simulation', 'Java']