Ninja opened a shikanjiβs stall to earn a living for him. In his stall, each shikanji drinks cost β5$β. Customers are standing in the form queue and each customer either pays β5$, 10$, 20$ β so now Ninja has to give them change so that each customer exactly pays β5$β.
So now help the ninja to find out whether he is able to charge exactly β5$β from each customer by providing them the change.
So your task is to write a code to check whether ninja can provide change to the customer if they paid extra to him. You will be provided with the βBILL_ARRβ array denoting the amount paid by each customer you have to return βTrueβ if it is possible else you have to return βFalseβ.
Example:
Suppose given βBILL_ARRβ array is { 5, 5, 5, 10, 20 } so we return βTrueβ for this test case as from first β3β customers we take β5$β from each customer then β4thβ customer give β10$β we give him β5$β back now we have β2β, β5$β note and β1β, β10$β note than β5thβ customer give β20$β so we give him back one β10$β and one β5$β note.
Input Format:
The first line of input contains a βTβ number of test cases.
The first line of each test case contains an integer βNβ i.e size of the array βBILL_ARRβ.
The second line of each test case contains an array βBILL_ARRβ, where βBILL_ARR[i]β denotes the money paid for βi-thβ shikanji.
Output Format:
For each test case, print βTrueβ if it is possible else print βFalseβ.
Note:
1. You donβt have any changes at starting.
2. You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10^2
1 <= N <= 10^3
BILL_ARR[i] = [ β5β, β10β, β20β ]
Where βTβ represents the number of test cases and βNβ represents the size of the array and βBILL_ARR[i]β represents the elements of the array.
Time Limit: 1 sec
2
5
5 10 5 20 5
4
5 20 5 10
True
False
Test Case 1:
For the first test case given βBILL_ARRβ is { 5, 10, 5, 20, 5 } so we print βTrueβ as we take β5$β from the first customer then β2ndβ customer gives us β10$β we give him back β5$β then β3rdβ customer give use β5$β then β4thβ customer give us β20$β now we give him back one β5$β note and one β10$β note.
Test Case 2:
For the second test case given βBILL_ARRβ is { 5, 20, 5, 10 } so we print βFalseβ as from the first customer we take β5$β note than β2ndβ customer give us β20$β note and we donβt have required change.
2
3
5 10 20
7
5 5 10 5 5 20 10
False
True
Test Case 1:
For the first test case given βBILL_ARRβ is { 5, 10, 20} so we print βFalseβ as we take β5$β from the first customer then β2ndβ customer gives us β10$β we give him back β5$β then β3rdβ customer give use β20$β but we will not be able to give him change back.
Test Case 2:
For the second test case given βBILL_ARRβ is { 5, 5, 10, 5, 5, 20, 10 } so we print βTrueβ as every transaction is possible.
Can you think of storing the count of notes?
Approach: The idea here is to use a β2β variables for count instead of a map and whenever we strike some note can check the count of variables to check whether we can able to give change or not.
The algorithm is as follows:
O(N ), where βNβ is the size of the array.
As we are iterating through our array using a simple loop.
O(1), we are using constant space.