👏Beats 100.00% of users with Java || ✅Simple & Easy Well Explained Solution 🔥💥
Intuition\nCount the number of 0\'s and 1\'s in the student.\nAfter that, give the sandwich of number 0 to the student who sees it, and when the sandwich of num
# Intuition\nCount the number of 0\'s and 1\'s in the student.\nAfter that, give the sandwich of number 0 to the student who sees it, and when the sandwich of number 0 is finished, even if it is seen, no one else can get it, so return the number that has increased by 1.\nGive the sandwich of number 1 to the student who sees it and when the sandwich of number 1 is finished, if it is seen, no one else can get it, so return it as many as 0 numbers have increased.\n\n# I Think This Can Help You(For Proof Click on the Image)\n[![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/c4362cf4-1d43-4d3f-8996-11b35b9372c7_1712559643.066665.png)](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/submissions/1226436575/?envType=daily-question&envId=2024-04-08)\n\n\n# Approach\n1. Counting students\' preferences:\n   - Two variables `zero_cnt` and `one_cnt` are initialized to count the number of students preferring sandwiches with filling 0 and 1 respectively.\n    ```\n    int zero_cnt=0,one_cnt=0;\n    ```\n   - The first loop iterates through the `students` array and increments the respective count variables based on the preference of each student.\n    ```\n    for(int i=0;i<students.length;i++){\n            if(students[i]==0){\n                zero_cnt++;\n            }else{\n                one_cnt++;\n            }\n        }\n    ```\n2. Distributing sandwiches:\n   - After counting the students\' preferences, the second loop iterates through the `sandwiches` array to distribute sandwiches.\n   - For each sandwich, it checks whether there are still students left who prefer that filling:\n     - If there are no students left who prefer filling 0 (if `sandwiches[i] == 0` and `zero_cnt == 0`), it returns the count of students who prefer filling 1 (`one_cnt`). This means that the remaining sandwiches can only be given to students who prefer filling 1.\n     - If there are no students left who prefer filling 1 (if `sandwiches[i] == 1` and `one_cnt == 0`), it returns the count of students who prefer filling 0 (`zero_cnt`). This means that the remaining sandwiches can only be given to students who prefer filling 0.\n     - If there are still students left who prefer the filling of the current sandwich, it decrements the count of students preferring that filling (`zero_cnt` or `one_cnt` accordingly).\n    ```\n    for(int i=0;i<sandwiches.length;i++){\n        if(sandwiches[i]==0 && zero_cnt==0){\n            return one_cnt;\n        }\n        if(sandwiches[i]==1 && one_cnt==0){\n            return zero_cnt;\n        }\n        if(sandwiches[i]==0){\n            zero_cnt--;\n        }else{\n            one_cnt--;\n        }\n    }\n    ```\n3. Handling any leftover sandwiches:\n   - If there are no more sandwiches or students, it returns 0, indicating that all students have received their preferred sandwiches.\n    ```\n    return 0;\n    ```\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int zero_cnt=0,one_cnt=0;\n        for(int i=0;i<students.length;i++){\n            if(students[i]==0){\n                zero_cnt++;\n            }else{\n                one_cnt++;\n            }\n        }\n        for(int i=0;i<sandwiches.length;i++){\n            if(sandwiches[i]==0 && zero_cnt==0){\n                return one_cnt;\n            }\n            if(sandwiches[i]==1 && one_cnt==0){\n                return zero_cnt;\n            }\n            if(sandwiches[i]==0){\n                zero_cnt--;\n            }else{\n                one_cnt--;\n            }\n        }\n        return 0;\n    }\n}\n```\n\n![_820a7945-fc34-4bfb-b379-3d81a750f95e.jpeg](https://assets.leetcode.com/users/images/debf4f0c-3ae0-455e-b56e-65dbe37c8a54_1712559852.5903344.jpeg)\n
['Array', 'Stack', 'Queue', 'Simulation', 'Java']