NINJA’S SHIKANJI

Easy
0/40
Average time to solve is 15m
profile
Contributed by
3 upvotes
Asked in companies
CultfitSAP Labs

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )

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

Sample Input 1:

2
5
5 10 5 20 5
4
5 20 5 10

Sample Output 1:

True
False

Explanation For Sample Input 1:

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.

Sample Input 2 :

2
3
5 10 20
7
5 5 10 5 5 20 10

Sample Output 2 :

False
True

Explanation For Sample Input 2:

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.
Hint

Can you think of storing the count of notes?

Approaches (1)
Brute Approach

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:

  1. Firstly we declare two variables β€˜FIVE’ and β€˜TEN’ for storing note value and their count.
  2. We start iterating our β€˜BILL_ARR’ and if we strike :
    • Note with the value β€˜5$’ we simply increase the count of β€˜5$’ in the variable β€˜FIVE’.
    • Note with value β€˜10$’ we check if the count of variable β€˜FIVE’ is greater than β€˜0’
      • If it exists we increase the count of note value β€˜10$’ by 1 and decrease the value of note value β€˜5$’ by β€˜1’.
      • Else we return β€˜False’.
    • Note with value β€˜20$’ we check if the count of variable β€˜TEN’ is greater than β€˜0’ and count of variable β€˜FIVE’ is greater than β€˜0’ or count of five is greater than β€˜2’
      • If it exists:
        • We check whether we give him 3, β€˜5$’ notes or 1, β€˜5$’ note and 1, β€˜10$’ note according to that we decrease the count.
      • Else we return β€˜False’.
  3. If we come out of the loop we return β€˜True’ in the end.
Time Complexity

O(N ), where β€˜N’ is the size of the array.

 

As we are iterating through our array using a simple loop.

Space Complexity

O(1), we are using constant space.

Code Solution
(100% EXP penalty)
NINJA’S SHIKANJI
Full screen
Console