Function to find Number of customers who could not get a computer | C ++ program to find number of customers who couldn 't get a resource. ; n is number of computers in cafe . ' seq ' is given sequence of customer entry , exit events ; seen [ i ] = 0 , indicates that customer ' i ' is not in cafe seen [ 1 ] = 1 , indicates that customer ' i ' is in cafe but computer is not assigned yet . seen [ 2 ] = 2 , indicates that customer ' i ' is in cafe and has occupied a computer . ; Initialize result which is number of customers who could not get any computer . ; Traverse the input sequence ; Find index of current character in seen [ 0. . .25 ] ; If First occurrence of ' seq [ i ] ' ; set the current character as seen ; If number of occupied computers is less than n , then assign a computer to new customer ; Set the current character as occupying a computer ; Else this customer cannot get a computer , increment result ; If this is second occurrence of ' seq [ i ] ' ; Decrement occupied only if this customer was using a computer ; Driver program
#include <iostream> NEW_LINE #include <cstring> NEW_LINE using namespace std ; #define MAX_CHAR  26 NEW_LINE int runCustomerSimulation ( int n , const char * seq ) { char seen [ MAX_CHAR ] = { 0 } ; int res = 0 ; for ( int i = 0 ; seq [ i ] ; i ++ ) { int ind = seq [ i ] - ' A ' ; if ( seen [ ind ] == 0 ) { seen [ ind ] = 1 ; if ( occupied < n ) { occupied ++ ; seen [ ind ] = 2 ; } else res ++ ; } else { if ( seen [ ind ] == 2 ) occupied -- ; seen [ ind ] = 0 ; } } return res ; } int main ( ) { cout << runCustomerSimulation ( 2 , " ABBAJJKZKZ " ) << endl ; cout << runCustomerSimulation ( 3 , " GACCBDDBAGEE " ) << endl ; cout << runCustomerSimulation ( 3 , " GACCBGDDBAEE " ) << endl ; cout << runCustomerSimulation ( 1 , " ABCBCA " ) << endl ; cout << runCustomerSimulation ( 1 , " ABCBCADEED " ) << endl ; return 0 ; }