Check if it is possible to serve customer queue with different notes
Given cost of the ticket ’25’ and an integer array ‘arr’ which holds the values of the notes people in a queue are having (either ’25’, ’50’ or ‘100’ Rs. Notes).
The task is to find whether it possible to sell tickets to the people in order, starting from 0 Rs.
Examples:
Input: arr = {25, 25, 50, 50}
Output: YES
You can give the 25 you received from the 1st customer
to the 3rd customer and then the 25 from the 2nd customer to the 4th.
Input: arr = {25, 100}
Output: NO
It is not possible to return the change to the 2nd customer.
Approach: Keep a track of the number of Rs. 25 and Rs. 50 notes that we currently have as ‘c25’ and ‘c50’ respectively. There is no need to keep track of the number of Rs. 100 notes as we can not return them to any customer. There are 3 possibilities now:
- If the customer pays Rs. 25: Increment c25, nothing has to be returned to the customer.
- If the customer pays Rs. 50: Rs. 25 has to be returned to the customer, check if c25>0 then increment c50 and decrement c25.
- If the customer pays Rs. 100: Rs. 75 has to be returned to the customer. There are two ways to do it, either using one Rs. 50 and one Rs. 25 note or using three Rs. 25 notes. We’ll prefer the first way so that if in future someone with Rs. 50 comes, we’ll still have our 25s left with us. Check whether it is possible and decrement the counts accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSellingPossible( int n, int a[])
{
int i, c25 = 0, c50 = 0;
for (i = 0; i < n; i++) {
if (a[i] == 25)
c25++;
else if (a[i] == 50) {
c50++;
if (c25 == 0)
break ;
c25--;
}
else {
if (c50 > 0 && c25 > 0) {
c50--;
c25--;
}
else if (c25 >= 3)
c25 -= 3;
else
break ;
}
}
if (i == n)
return true ;
else
return false ;
}
int main()
{
int a[] = { 25, 25, 50, 100 };
int n = sizeof (a) / sizeof (a[0]);
if (isSellingPossible(n, a)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
}
|
Java
class GFG
{
static boolean isSellingPossible( int n,
int a[])
{
int i, c25 = 0 , c50 = 0 ;
for (i = 0 ; i < n; i++)
{
if (a[i] == 25 )
c25++;
else if (a[i] == 50 )
{
c50++;
if (c25 == 0 )
break ;
c25--;
}
else
{
if (c50 > 0 && c25 > 0 )
{
c50--;
c25--;
}
else if (c25 >= 3 )
c25 -= 3 ;
else
break ;
}
}
if (i == n)
return true ;
else
return false ;
}
public static void main(String []args)
{
int a[] = { 25 , 25 , 50 , 100 };
int n = a.length;
if (isSellingPossible(n, a))
{
System.out.println( "YES" );
}
else
{
System.out.println( "NO" );
}
}
}
|
Python3
def isSellingPossible(n, a):
c25 = 0 ;
c50 = 0 ;
i = 0 ;
while (i < n):
if (a[i] = = 25 ):
c25 + = 1 ;
elif (a[i] = = 50 ):
c50 + = 1 ;
if (c25 = = 0 ):
break ;
c25 - = 1 ;
else :
if (c50 > 0 and c25 > 0 ):
c50 - = 1 ;
c25 - = 1 ;
elif (c25 > = 3 ):
c25 - = 3 ;
else :
break ;
i + = 1 ;
if (i = = n):
return True ;
else :
return False ;
a = [ 25 , 25 , 50 , 100 ];
n = len (a);
if (isSellingPossible(n, a)):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
static bool isSellingPossible( int n, int []a)
{
int i, c25 = 0, c50 = 0;
for (i = 0; i < n; i++)
{
if (a[i] == 25)
c25++;
else if (a[i] == 50)
{
c50++;
if (c25 == 0)
break ;
c25--;
}
else
{
if (c50 > 0 && c25 > 0)
{
c50--;
c25--;
}
else if (c25 >= 3)
c25 -= 3;
else
break ;
}
}
if (i == n)
return true ;
else
return false ;
}
public static void Main()
{
int []a = { 25, 25, 50, 100 };
int n = a.Length;
if (isSellingPossible(n, a))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
PHP
<?php
function isSellingPossible( $n , $a )
{
$c25 = 0;
$c50 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 25)
$c25 ++;
else if ( $a [ $i ] == 50)
{
$c50 ++;
if ( $c25 == 0)
break ;
$c25 --;
}
else
{
if ( $c50 > 0 && $c25 > 0)
{
$c50 --;
$c25 --;
}
else if ( $c25 >= 3)
$c25 -= 3;
else
break ;
}
}
if ( $i == $n )
return true;
else
return false;
}
$a = array ( 25, 25, 50, 100 );
$n = sizeof( $a );
if (isSellingPossible( $n , $a ))
{
echo "YES" ;
}
else
{
echo "NO" ;
}
?>
|
Javascript
<script>
function isSellingPossible(n, a)
{
let i, c25 = 0, c50 = 0;
for (i = 0; i < n; i++)
{
if (a[i] == 25)
c25++;
else if (a[i] == 50)
{
c50++;
if (c25 == 0)
break ;
c25--;
}
else
{
if (c50 > 0 && c25 > 0)
{
c50--;
c25--;
}
else if (c25 >= 3)
c25 -= 3;
else
break ;
}
}
if (i == n)
return true ;
else
return false ;
}
let a = [ 25, 25, 50, 100 ];
let n = a.length;
if (isSellingPossible(n, a))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...