C++ ✅ Easy Solution Without Queue 🔥 Beats 100% Users 🔥
\n\n# Intuition\nThe function timeRequiredToBuy calculates the total time for the person at position k to finish buying all their tickets. The approach simulate
![Screenshot (196).png](https://assets.leetcode.com/users/images/9333dda9-d011-447b-8e02-741929cfb6c6_1721982475.8747003.png)\n\n# Intuition\nThe function timeRequiredToBuy calculates the total time for the person at position k to finish buying all their tickets. The approach simulates the ticket buying process by looping through the line of people. In each iteration, for every person who still needs tickets (i.e., v[i] > 0), the code decrements their ticket count by one and increments the time counter c. This continues until the person at position k has no tickets left (v[k] == 0). The break statement ensures the loop terminates early if the person at position k finishes buying their tickets during the current pass through the line.\n\n# Approach\n1. **Initialization :**\n- The function timeRequiredToBuy takes a vector v of integers, where v[i] represents the number of tickets the i-th person wants to buy, and an integer k representing the index of the person of interest.\n- A counter c is initialized to 0 to keep track of the total time.\n\n2. **Outer Loop :** The outer while loop runs until the person at index k has bought all their tickets (v[k] == 0).\n\n3. **Inner Loop :**\n- The inner for loop iterates through each person in the queue.\n \n- If the current person (i-th person) has tickets to buy (v[i] > 0):\na) They buy one ticket, so v[i] is decremented by 1.\nb) The counter c is incremented by 1 to represent the time taken for this transaction.\n- After each ticket purchase, it checks if the person at index k has finished buying all their tickets (v[k] == 0). If so, it breaks out of the loop to avoid unnecessary iterations.\n\n4. **Return :** Once the person at index k has bought all their tickets, the function returns the total time c taken for this to happen.\n \n# Example Walkthrough\n\nFor instance, consider v = [2, 3, 2] and k = 2:\n\nInitial state: v = [2, 3, 2], c = 0.\n\n1. **First iteration of the while loop :**\n- v = [1, 3, 2], c = 1 (first person buys 1 ticket).\n- v = [1, 2, 2], c = 2 (second person buys 1 ticket).\n- v = [1, 2, 1], c = 3 (third person buys 1 ticket).\n \n2. **Second iteration of the while loop :**\n- v = [0, 2, 1], c = 4 (first person buys their last ticket).\n- v = [0, 1, 1], c = 5 (second person buys 1 ticket).\n- v = [0, 1, 0], c = 6 (third person buys their last ticket and now v[k] == 0).\n\nSo, the function will return 6, which is the total time taken for the person at position k to finish buying their tickets.\n\n# Time Complexity\n - **Best Case :** O(n) where n is Size of Vector\n- **Average Case :** The total number of iterations will depend on the average value of the array elements. If the average number of tickets each person buys is T/n, where T is the total number of tickets, the time complexity would be approximately O(n.T/n) = O(T).\n- **Worst Case :** If every person wants a large number of tickets, each requiring a full pass through the array, the time complexity is O(n.T).\n\n# Space complexity: O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& v, int k) {\n        int c=0;\n        while(v[k]!=0){\n            for(int i =0;i<v.size();i++){\n                if(v[i]>0){\n                    v[i]--;\n                    c++;\n                }\n                if(v[k]==0) break;\n            }\n        }\n        return c;\n    }\n};\n```
['Array', 'C++']