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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns
// true is selling of
// the tickets is possible
bool isSellingPossible(int n, int a[])
{
    int i, c25 = 0, c50 = 0;
    for (i = 0; i < n; i++) {
  
        // Nothing to return
        // to the customer
        if (a[i] == 25)
            c25++;
        else if (a[i] == 50) {
            c50++;
  
            // Check if 25 can be
            // returned to customer.
            if (c25 == 0)
                break;
            c25--;
        }
        else {
  
            // Try returning one
            // 50 and one 25
            if (c50 > 0 && c25 > 0) {
                c50--;
                c25--;
            }
  
            // Try returning three 25
            else if (c25 >= 3)
                c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if (i == n)
        return true;
    else
        return false;
}
  
// Driver Program to
// test above function
int main()
{
    int a[] = { 25, 25, 50, 100 };
    int n = sizeof(a) / sizeof(a[0]);
  
    if (isSellingPossible(n, a)) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
// Function that returns
// true is selling of
// the tickets is possible
static boolean isSellingPossible(int n,
                                 int a[])
{
    int i, c25 = 0, c50 = 0;
    for (i = 0; i < n; i++)
    {
  
        // Nothing to return
        // to the customer
        if (a[i] == 25)
            c25++;
        else if (a[i] == 50)
        {
            c50++;
  
            // Check if 25 can be
            // returned to customer.
            if (c25 == 0)
                break;
            c25--;
        }
        else
        {
  
            // Try returning one
            // 50 and one 25
            if (c50 > 0 && c25 > 0
            {
                c50--;
                c25--;
            }
  
            // Try returning three 25
            else if (c25 >= 3)
                c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if (i == n)
        return true;
    else
        return false;
}
  
// Driver Code
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");
    }
}
}
  
// This code is contributed
// by ihritik

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function that returns true is selling
# of the tickets is possible
def isSellingPossible(n, a):
  
    c25 = 0;
    c50 = 0;
    i = 0;
    while(i < n): 
      
  
        # Nothing to return to the customer
        if (a[i] == 25):
            c25 += 1;
        elif (a[i] == 50): 
          
            c50 += 1;
  
            # Check if 25 can be returned 
            # to customer.
            if (c25 == 0):
                break;
            c25 -= 1;
          
        else:
      
            # Try returning one
            # 50 and one 25
            if (c50 > 0 and c25 > 0):
              
                c50 -= 1;
                c25 -= 1;
              
            # Try returning three 25
            elif (c25 >= 3):
                c25 -= 3;
            else:
                break;
        i += 1;
      
    # If the loop did not break,
    # all the tickets were sold
    if (i == n):
        return True;
    else:
        return False;
  
# Driver Code
a = [ 25, 25, 50, 100 ];
n = len(a);
  
if (isSellingPossible(n, a)):
    print("YES");
else:
    print("NO");
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
// Function that returns
// true is selling of
// the tickets is possible
static bool isSellingPossible(int n, int []a)
{
    int i, c25 = 0, c50 = 0;
    for (i = 0; i < n; i++) 
    {
  
        // Nothing to return
        // to the customer
        if (a[i] == 25)
            c25++;
        else if (a[i] == 50)
        {
            c50++;
  
            // Check if 25 can be
            // returned to customer.
            if (c25 == 0)
                break;
            c25--;
        }
        else 
        {
  
            // Try returning one
            // 50 and one 25
            if (c50 > 0 && c25 > 0)
            {
                c50--;
                c25--;
            }
  
            // Try returning three 25
            else if (c25 >= 3)
                c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if (i == n)
        return true;
    else
        return false;
}
  
// Driver Code
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");
    }
}
}
  
// This code is contributed
// by ihritik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function that returns
// true is selling of
// the tickets is possible
function isSellingPossible($n, $a)
{
    $c25 = 0;
    $c50 = 0;
    for ($i = 0; $i < $n; $i++) 
    {
  
        // Nothing to return
        // to the customer
        if ($a[$i] == 25)
            $c25++;
        else if ($a[$i] == 50) 
        {
            $c50++;
  
            // Check if 25 can be
            // returned to customer.
            if ($c25 == 0)
                break;
            $c25--;
        }
        else 
        {
  
            // Try returning one
            // 50 and one 25
            if ($c50 > 0 && $c25 > 0)
            {
                $c50--;
                $c25--;
            }
  
            // Try returning three 25
            else if ($c25 >= 3)
                $c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if ($i == $n)
        return true;
    else
        return false;
}
  
// Driver Code
$a = array( 25, 25, 50, 100 );
$n = sizeof($a);
  
if (isSellingPossible($n, $a))
{
    echo "YES";
}
else
{
    echo "NO";
}
  
// This code is contributed
// by ihritik
?>

chevron_right


Output:

YES

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.