[JAVA] [Beginner Friendly] Solution, Time O(N) Space O(1)
Approach #1\nThe question states that "This(the queue) continues until none of the queue students want to take the top sandwich and are thus unable to eat.". An
**Approach #1**\nThe question states that *"This(the queue) continues until none of the queue students want to take the top sandwich and are thus unable to eat."*. And that\'s what you might want to do!!!\nCreate a queue and run a loop until \n1). either all the sandwiches are taken up *(then return zero)* or \n2). no one in the queue is ready to take the top sandwich *(then return the number of students left in the queue)*.\n\n**Approach #2**\nDiving a bit into the question, the repetition of the queue indicates a circular fashion. So to check if the sandwich at the top of the stack is going to be consumed we only need to check that if there is a student anywhere in the queue who wants a sandwich of the same type or not.\n\nAlso, thinking about the above explanation in **Approach#1**, the point at which we are going to meet the 2nd condition is when there is no student in the queue who wants the sandwich at the top of the stack i.e. either the condition that sandwich of type **1** is at the top and all students in the queue want a sandwich of type **0** or vice versa is true.\n\nKeeping the above two in mind, all we need to do is calculate the no. of students who need the sandwich of type0 and type1 separately, then for every `sandwich[i]` check if there is a student in the queue who need the sandwich of the same type. If yes allocate the sandwich to him and decrease the count of students who want the sandwich of the same type. Else as there is no one in the queue who wants the sandwich, all the remaining students will not be able to eat lunch!\n\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int ones = 0; //count of students who prefer type1\n        int zeros = 0; //count of students who prefer type0\n\t\t\n        for(int stud : students){\n            if(stud == 0) zeros++;\n            else ones++;\n        }\n        \n        // for each sandwich in sandwiches\n        for(int sandwich : sandwiches){\n            if(sandwich == 0){  // if sandwich is of type0\n                if(zeros == 0){ // if no student want a type0 sandwich\n                    return ones;\n                }\n                zeros--;\n            }\n            else{  // if sandwich is of type1\n                if(ones == 0){  // if no student want a type1 sandwich \n                    return zeros;\n                }\n                ones--;\n            }\n        }\n        return 0;\n    }\n}\n```\nP.S. This was my first post. \nHope you\'ll find the solution useful.\n**Happy Coding :)**
['Java']