Betas 100✅||3 Approaches🔥🔥 with explanation✅✅
In this Question, it basically refers whether any student in the  array  can take the sandwich or not.if a particular type of sandwich is not taken then, (As th
**In this Question, it basically refers whether any student in the  array  can take the sandwich or not.if a particular type of sandwich is not taken then, (As the student count =sandwich count) all the corresponding sandwiches will remain untouched,thus the corresponding students will not eat anything.**\n\n**Approach 1: Through visited array O(n^2):**\n\n**we will check whether ```any student``` in the array other than visited would  be able to eat the sandwich at the ```specified ind```**\n\n **If ```yes``` mark the visted as one and move to next sandwich..**\n\n **If ```false``` then no one is able to eat the specified sandwich type the ```flag``` will be marked default at``` 0``` and the remaining sandwiches,(students) wil be returned as output. thus the approach**\n\n``` Java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int n=students.length;\n        int visited[]=new int [n];\n        for(int i=0;i<n;i++){\n            int flag=0;\n            for(int j=0;j<n;j++){\n                if(students[j]==sandwiches[i]&&visited[j]==0) {\n                    visited[j]=1;\n                    flag=1;\n                    break;\n                }\n            }\n            if(flag==0) return n-i;\n        }\n        return 0;\n    }\n}\n```\n``` Python3 []\nclass Solution:\n    def countStudents(self, students, sandwiches):\n        n = len(students)\n        visited = [0] * n\n        for i in range(n):\n            flag = 0\n            for j in range(n):\n                if students[j] == sandwiches[i] and visited[j] == 0:\n                    visited[j] = 1\n                    flag = 1\n                    break\n            if flag == 0:\n                return n - i\n        return 0\n```\n``` C++ []\n\nclass Solution {\npublic:\n    int countStudents(std::vector<int>& students, std::vector<int>& sandwiches) {\n        int n = students.size();\n        std::vector<int> visited(n, 0);\n        for (int i = 0; i < n; ++i) {\n            int flag = 0;\n            for (int j = 0; j < n; ++j) {\n                if (students[j] == sandwiches[i] && visited[j] == 0) {\n                    visited[j] = 1;\n                    flag = 1;\n                    break;\n                }\n            }\n            if (flag == 0) return n - i;\n        }\n        return 0;\n    }\n};\n```\n``` JavaScript []\nclass Solution {\n    countStudents(students, sandwiches) {\n        const n = students.length;\n        const visited = new Array(n).fill(0);\n        for (let i = 0; i < n; i++) {\n            let flag = 0;\n            for (let j = 0; j < n; j++) {\n                if (students[j] === sandwiches[i] && visited[j] === 0) {\n                    visited[j] = 1;\n                    flag = 1;\n                    break;\n                }\n            }\n            if (flag === 0) return n - i;\n        }\n        return 0;\n    }\n}\n```\n\n**Approach 2: Through Queue**\n\n**we can basically follow this question in Queue.the one who is being changed dynamically and adding to the last of the queue.**\n\n**so,we can just ```pop()``` each element in the queue and check with the ordered sandwich array.**\n\n**if it is matched we will iterate to the next element and repeat the operation.**\n```Java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int n=students.length;\n        Queue <Integer> st=new LinkedList<>();\n        for(int i:students){\n            st.add(i);\n        }\n        int count=0,j=0;\n        while(!st.isEmpty()){\n            int ref=st.poll();\n            if(ref==sandwiches[j]){\n                j++;\n                count=0;\n            }\n            else{\n                st.add(ref);\n                count++;\n            }\n            if(count==n) return n-j;\n        }\n        return 0;\n    }\n}\n```\n\n``` Python3 []\n\nclass Solution:\n    def countStudents(self, students, sandwiches):\n        n = len(students)\n        st = deque(students)\n        count = 0\n        j = 0\n        while st:\n            ref = st.popleft()\n            if ref == sandwiches[j]:\n                j += 1\n                count = 0\n            else:\n                st.append(ref)\n                count += 1\n            if count == n:\n                return n - j\n        return 0\n```\n``` C++ []\nclass Solution {\npublic:\n    int countStudents(std::vector<int>& students, std::vector<int>& sandwiches) {\n        int n = students.size();\n        std::queue<int> st;\n        for (int i : students) {\n            st.push(i);\n        }\n        int count = 0, j = 0;\n        while (!st.empty()) {\n            int ref = st.front();\n            st.pop();\n            if (ref == sandwiches[j]) {\n                j++;\n                count = 0;\n            } else {\n                st.push(ref);\n                count++;\n            }\n            if (count == n)\n                return n - j;\n        }\n        return 0;\n    }\n};\n```\n**Approach 3: Through count (most optimal <O(n)):\nwe can just count the no of zeroes and ones in the students array**.\n\n**then we will iterate through out the sandwich array minimizing ```zero_count``` and ```ones_count``` at beach step .when the ```count<0```  we\'ll simply break and return the sandwich availble indicating the students who arent eaten yet.**\n\n**Thats it guys!!**\n\n``` Java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int n=students.length;\n        int ones_count=0,zero_count=0;\n        for(int p:students){\n            if(p==1) ones_count++;\n            else zero_count++;\n        }\n         for(int i=0;i<n;i++){\n            if(sandwiches[i]==1&&ones_count>0) ones_count-=1;\n            else if(sandwiches[i]==0&&zero_count>0) zero_count-=1;\n            else return n-i;\n        }\n        return 0;\n\n    }\n}\n``` \n``` Python3 []\nclass Solution:\n    def countStudents(self, students, sandwiches):\n        n = len(students)\n        ones_count = students.count(1)\n        zero_count = n - ones_count\n        for i in range(0,len(sandwiches)):\n            if sandwiches[i] == 1 and ones_count > 0:\n                ones_count -= 1\n            elif sandwiches[i] == 0 and zero_count > 0:\n                zero_count -= 1\n            else:\n                return n - i\n        return 0\n```\n``` C++ []\nclass Solution {\npublic:\n    int countStudents(std::vector<int>& students, std::vector<int>& sandwiches) {\n        int n = students.size();\n        int ones_count = 0, zero_count = 0;\n        for (int p : students) {\n            if (p == 1) ones_count++;\n            else zero_count++;\n        }\n        for (int i = 0; i < n; i++) {\n            if (sandwiches[i] == 1 && ones_count > 0) ones_count--;\n            else if (sandwiches[i] == 0 && zero_count > 0) zero_count--;\n            else return n - i;\n        }\n        return 0;\n    }\n};\n```\n# If this helps,Please upVote!!\n
['Array', 'Stack', 'Queue', 'C++', 'Java', 'Python3', 'JavaScript']