Beginner Friendly!!!🔥🔥 Beats 100% Simple iterative approach no special data structure used !! ⭐⭐⭐⭐
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nI have tried to do the solution in single loop so divide into two halves one incl
\n![image.png](https://assets.leetcode.com/users/images/47598bef-855f-48cf-b64e-2e655b042ebf_1712640052.3000953.png)\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI have tried to do the solution in single loop so divide into two halves one including target and earlier ones and the other having remaining ones .\n# Approach\n<!-- Describe your approach to solving the problem. -->\nint target = tickets[k];: This line fetches the price of the ticket at the position k in the queue. This will be the maximum price you\'ll pay.\n\nint ans = 0;: This initializes the variable ans to store the total time required to buy all the tickets.\n\nThe first loop (for(;i<=k;i++)) iterates from the beginning of the queue up to your position k. Inside this loop:\n\nIf the price of the ticket at the current position (tickets[i]) is greater than target, it means you\'ll pay target.\nOtherwise, you pay the actual price of the ticket (tickets[i]).\nThe corresponding time required to buy the ticket is added to ans.\nAfter the first loop, i will be equal to k+1, pointing to the next ticket after your position in the queue.\n\nThe second loop (for(;i<tickets.size();i++)) iterates from the position after k until the end of the queue. Inside this loop:\n\nIf the price of the ticket at the current position (tickets[i]) is greater than or equal to target, it means you\'ll pay target - 1 because you can\'t exceed target.\nOtherwise, you pay the actual price of the ticket (tickets[i]).\nThe corresponding time required to buy the ticket is added to ans.\nFinally, the function returns the total time required (ans).\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int target = tickets[k] ; \n        int ans = 0 ; \n        int i=0;\n        for(;i<=k;i++){\n            if(tickets[i]>target){\n                ans += target ; \n            }\n            else ans += tickets[i] ; \n        }\n        for(;i<tickets.size();i++){\n            if(tickets[i]>=target){\n                ans += target-1 ; \n            }\n            else ans += tickets[i] ; \n        }\n        return ans ; \n    }\n};\n```
['C++']