A corporate security checkpoint issues entry credentials to a line of employees each morning. Every employee needs exactly one of two credential types, labeled 0 or 1. A kiosk holds a fixed stack of credentials to dispense today, also labeled 0 or 1, ordered from the one on top (dispensed first) to the one at the bottom (dispensed last).
Employees stand in a line. Repeatedly, the kiosk offers the credential currently on top of its stack to the employee at the front of the line:
This continues until either the kiosk runs out of credentials, or every employee still in line has been offered the current top credential once already without a single match -- meaning a full lap of the remaining line has passed with nobody taking it -- at which point the process halts for the day.
Given the line order and the stack order, determine how many employees end the day without a credential.
Line 1: a single integer n. Line 2: n integers, each 0 or 1 -- the employees' required credential types, listed from the front of the line to the back. Line 3: n integers, each 0 or 1 -- the credential stack, listed from the top (dispensed first) to the bottom (dispensed last).
A single integer -- the number of employees who never receive a credential.
Example 1
Input
4 0 1 0 1 0 1 0 1
Expected
0
Explanation
Employees want, in line order: 0,1,0,1 (two of each type). The stack offers 0,1,0,1 from top to bottom. Each offered credential always matches the employee currently at the front (or, equivalently, there are still employees of that type waiting), so all four credentials get dispensed in order and every employee leaves the line. No one is left stuck, so the answer is 0.
Example 2
Input
6 1 1 1 0 0 1 1 0 0 0 1 1
Expected
3
Explanation
Among the 6 employees, 4 want type 1 and 2 want type 0. Because rotating the line never changes which types are still waiting, only the remaining counts per type matter as each credential is offered: top=1 (4 want it) -> dispensed, 3 of type 1 left; next=0 (2 want it) -> dispensed, 1 of type 0 left; next=0 (1 wants it) -> dispensed, 0 of type 0 left; next=0, but now nobody in line wants a 0 anymore -- a full lap confirms no match, so the kiosk stops. 3 employees (all still wanting type 1) never got served, so the answer is 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →