BEST JAVA SOLUTION TILL DATE RUNTIME->1ms , EASY AND UNDERSTANDABLE JAVA CODE
Intuition\n Describe your first thoughts on how to solve this problem. \nBasic knowledge of queue required\n\n# Approach\n Describe your approach to solving the
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasic knowledge of queue required\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n We start by creating a queue (stud) to represent students waiting in line. We add each student\'s preference to this queue.\nServing Process Simulation: We simulate the process of serving sandwiches to students using a while loop. We keep track of which sandwich to serve next using an index j.\n Inside the loop, we iterate through the students in the queue. If the top student\'s preference matches the sandwich at index j, we serve that student (remove from the queue) and move to the next sandwich (j++). We keep track of the number of students served in each iteration.\n If no students are served in an iteration, it means we couldn\'t serve any more students because no sandwich preference matches. In this case, we exit the loop.\nFinally, we return the number of students remaining in the queue after the simulation, which represents the students who couldn\'t be served.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sand) {\n        Queue<Integer> stud=new LinkedList<>();\n        for(int i:students) stud.offer(i);\n        int j=0;\n        boolean flag=true;\n        while(flag){\n            int c=0;\n        for(int i=0;i<stud.size();i++){\n            if(stud.peek()==sand[j]){\n                stud.poll();\n                j++;\n                c++;\n            }else{\n                int x=stud.poll();\n                stud.offer(x);\n            }\n         }\n         if(c>0) flag =true;\n         else {\n            c=0;\n            flag=false;\n         }\n        }\n        return stud.size();\n    }\n}\n```
['Java']