Number of Students Unable to Eat Lunch

A cafeteria offers circular (`0`) and square (`1`) sandwiches. The students stand in a queue and each prefers one shape; the sandwiches sit in a stack. The student at the front takes the top sandwich if it is the shape they prefer and leaves; otherwise they move to the back of the queue. This repeats until nobody left in the queue wants the top sandwich. Given `students` (front of the queue first) and `sandwiches` (top of the stack first), return how many students are unable to eat.

30:00

Number of Students Unable to Eat Lunch
easy
Free written solution
Topics
Companies

A cafeteria offers circular (0) and square (1) sandwiches. The students stand in a queue and each prefers one shape; the sandwiches sit in a stack. The student at the front takes the top sandwich if it is the shape they prefer and leaves; otherwise they move to the back of the queue. This repeats until nobody left in the queue wants the top sandwich. Given students (front of the queue first) and sandwiches (top of the stack first), return how many students are unable to eat.

Example 1:
Input: {"students":[1,1,0,0],"sandwiches":[0,1,0,1]}
Output: 0
Constraints:
  • 1≤students.length,sandwiches.length≤1001 \leq \text{students.length}, \text{sandwiches.length} \leq 100

  • students.length=sandwiches.length\text{students.length} = \text{sandwiches.length}

  • sandwiches[i]\text{sandwiches}[i] is 00 or 11.

  • students[i]\text{students}[i] is 00 or 11.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"students":[1,1,0,0],"sandwiches":[0,1,0,1]}

count[0]: want circular

0

count[1]: want square

0

stack

queue (front first)

4 in line

#0

#1

#2

#3

The line keeps rotating, so the order of students does not matter: a sandwich gets eaten as long as someone in line wants it. Only the counts matter.

ate 0 / 4
Variables
VariableValue
i0
sandwich0
count[0,0]
DepthFunction Call
Stack empty
0/10