Java solution using Stack and Queue
\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        Stack<Integer> stack = new Stack<>();\n        Queue<Integer> queu
```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        Stack<Integer> stack = new Stack<>();\n        Queue<Integer> queue = new ArrayDeque<>();\n        \n        for(int i=sandwiches.length-1; i>=0; i--) {\n            stack.push(sandwiches[i]);\n        }\n        \n        for(int i=0; i<students.length; i++) {\n            queue.add(students[i]);\n        }\n        \n        int topSandwichRejectedBy = 0;\n        \n        while(true) {\n            if(topSandwichRejectedBy == queue.size())\n                break;\n        \n            if(queue.peek() == stack.peek()) {\n                queue.remove();\n                stack.pop();\n                topSandwichRejectedBy = 0;\n            } else {\n                topSandwichRejectedBy++;\n                queue.add(queue.remove());\n            }\n        }\n        \n        return queue.size();\n    }\n}\n```
[]