[Java] O(n) || One Pass || Explained
Explanation / Approach:\nBrute force is somewhat easy to come up with. You just need to iterate as long as tickets[k] != 0.\n\nFor an optimized approach :\n1. A
**Explanation / Approach:**\nBrute force is somewhat easy to come up with. You just need to iterate as long as tickets[k] != 0.\n\n**For an optimized approach :**\n1. Add min(tickets[i],tickets[k]) upto k (inclusive).\n2. Add min(tickets[i],tickets[k] - 1) after k.\n3. Return the count.\n\nTime : O(n) , Space : O(1)\n```\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int res = 0;\n        for(int i = 0;i<tickets.length;i++){\n            if(i <= k){\n                res += Math.min(tickets[k],tickets[i]);\n            }else{\n                res += Math.min(tickets[k] - 1,tickets[i]);\n            }\n        }\n        return res;\n    }\n}\n```
['Java']