C++ | Simple Queue Approach Explanation | 0ms , 100% Faster
Logic Discussion:\n\nFrom the problem description, we can clearly say we have to use queue data structure to simulate the given situation.\nOur answer would be 
## Logic Discussion:\n\nFrom the problem description, we can clearly say we have to use queue data structure to simulate the given situation.\nOur answer would be the size of queue in end. The main challenge is, **How to find when we have stop for iterations of queue**.\n\nThe logical comes here in our mind is, to keep track of rotations, i.e. how many front elements of queue have been popped and pushed again and again. Once our rotations would be equal to size of queue, that means, now there are the students in queue, which won\'t be able to have lunch.\n\nAlso, when a student gets the sandwich of his choice, we have to set rotations to 0. Because that means size of queue has been changed and we have to re-count the rotations.\n\n## Approach:\n\n- Declare a queue and push all the student choices in it.\n- Initialize a rotations variable with 0 and a counter variable (i) with 0.\n- Traverse the queue until it is empty and rotations is less than size of queue.\n\t- If front value of queue == sandwiches at i index\n\t\t- Pop it from queue, increase the counter variable i and set rotations = 0.\n\t- Else take it from queue, push it back and pop it from front. Increase the number of rotations.\n- Return size of queue. \n\nMy submission:\n\n```\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int size = students.size();\n        queue<int> student_choice;\n        for(int i = 0 ; i < size ; ++i){\n            student_choice.push(students[i]);\n        }\n        int rotations = 0 , i = 0;\n        while(student_choice.size() && rotations < student_choice.size()){\n            if(student_choice.front() == sandwiches[i]){\n                student_choice.pop();\n                i++;\n                rotations = 0;\n            } else {\n                int choice = student_choice.front();\n                student_choice.pop();\n                student_choice.push(choice);\n                rotations++;\n            }\n        }\n        return student_choice.size();\n    }\n};\n```\n**PLEASE UPVOTE IF YOU LIKED IT, IT REALLY MOTIVATES :)**
['Queue', 'C']