Simulation And It's Optimisation | Java | C++
Intuition, approach, and complexity dicussed in video solution in detail\nhttps://youtu.be/2RnD64ew6G8\n\n# Code\n- Approach 1\n\nJava\n\nclass Solution {\n    
# Intuition, approach, and complexity dicussed in video solution in detail\nhttps://youtu.be/2RnD64ew6G8\n\n# Code\n- Approach 1\n\nJava\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int sandPtr = 0, stdPtr = 0, size = students.length;\n        while(sandPtr < size){\n            if(sandwiches[sandPtr] == students[stdPtr]){\n                students[stdPtr] = -1;\n                stdPtr = (stdPtr + 1 ) % size;\n                sandPtr++;\n            }else{\n                int checkWithCnt  = 0;\n                while(checkWithCnt < size && students[stdPtr] != sandwiches[sandPtr]){\n                    stdPtr = (stdPtr + 1 ) % size;\n                    checkWithCnt++;\n                }\n                if(checkWithCnt == size)break;\n            }\n        }\n        return size - sandPtr;\n    }\n}\n```\nC++\n```\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int sandPtr = 0, stdPtr = 0, size = students.size();\n        while (sandPtr < size) {\n            if (sandwiches[sandPtr] == students[stdPtr]) {\n                students[stdPtr] = -1;\n                stdPtr = (stdPtr + 1) % size;\n                sandPtr++;\n            } else {\n                int checkWithCnt = 0;\n                while (checkWithCnt < size && students[stdPtr] != sandwiches[sandPtr]) {\n                    stdPtr = (stdPtr + 1) % size;\n                    checkWithCnt++;\n                }\n                if (checkWithCnt == size) break;\n            }\n        }\n        return size - sandPtr;\n    }\n};\n```\n- Approach 2\nJava\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int size = students.length;\n        int zeroSandNeed = 0, oneSandNeed = 0;\n        \n        for (int sandNeed : students) {\n            if (sandNeed == 0) {\n                zeroSandNeed++;\n            } else {\n                oneSandNeed++;\n            }\n        }\n        \n        for (int indx = 0; indx < size; indx++) {\n            if (sandwiches[indx] == 1) {\n                if (oneSandNeed == 0) {\n                    return size - indx;\n                }\n                oneSandNeed--;\n            } else {\n                if (zeroSandNeed == 0) {\n                    return size - indx;\n                }\n                zeroSandNeed--;\n            }\n        }\n        \n        return 0;\n    }\n}\n```\n\nC++\n```\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int size = students.size(), zeroSandNeed = 0, oneSandNeed = 0;\n        for(auto sandNeed : students){\n            if(sandNeed == 0)zeroSandNeed++;\n            else oneSandNeed++;\n        }\n        for(int indx = 0; indx < size; indx++){\n            if(sandwiches[indx] == 1){\n                if(oneSandNeed == 0)return size - indx;\n                oneSandNeed--;\n            }else{\n                if(zeroSandNeed == 0)return size - indx;\n                zeroSandNeed--;\n            }\n        }\n        return 0;\n    }\n};\n```
['Array', 'Stack', 'Queue', 'Simulation', 'C++', 'Java']