Skip to main content
Back to problems
#1700
Easy Algorithms

Number of students unable to eat lunch

Array Stack Queue Simulation
79.4% acceptance
Feb 25, 2026
2706
291
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches. The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step: If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue. Otherwise, they will leave it and go to the queue's end. This continues until none of the queue students want to take the top sandwich and are thus unable to eat. You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_students(students: Vec<i32>, sandwiches: Vec<i32>) -> i32 {
    let mut count = [0i32; 2];
    for &s in &students {
      count[s as usize] += 1;
    }
    for s in sandwiches {
      if count[s as usize] == 0 {
        return count[0] + count[1];
      }
      count[s as usize] -= 1;
    }
    0
  }
}