Simple code without using Builtin functions.
Complexity\n- Time complexity:\nIf there are n people in the queue and the maximum number of tickets any person has is t, the time complexity is O(n * t).\n\n- 
# Complexity\n- Time complexity:\nIf there are n people in the queue and the maximum number of tickets any person has is t, the time complexity is $$O(n * t)$$.\n\n- Space complexity:\nSince the dominant factor in memory usage is the tickets array, the overall space complexity of the code is $$O(n)$$, where \'n\' is the size of the input array. This is considered linear space complexity, meaning the space usage grows proportionally with the input size.\n\n# Code\n```\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int count = 0 ;\n        while(tickets[k]!=0 ){\n            for(int i=0;i<tickets.length;i++){\n                if(tickets[i]>0 && tickets[k]>0){\n                    tickets[i]-=1;\n                    count++;\n                }\n            }\n        }\n        return count ;\n    }\n}\n```
['Java']