Function to find Number of customers who could not get a computer | JAVA 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 . ; To keep track of occupied computers ; 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
class GFG { static int MAX_CHAR = 26 ; static int runCustomerSimulation ( int n , char [ ] seq ) { char [ ] seen = new char [ MAX_CHAR ] ; int res = 0 ; int occupied = 0 ; for ( int i = 0 ; i < seq . length ; 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 ; } public static void main ( String [ ] args ) { System . out . println ( runCustomerSimulation ( 2 , " ABBAJJKZKZ " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 3 , " GACCBDDBAGEE " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 3 , " GACCBGDDBAEE " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 1 , " ABCBCA " . toCharArray ( ) ) ) ; System . out . println ( runCustomerSimulation ( 1 , " ABCBCADEED " . toCharArray ( ) ) ) ; } }