💯JAVA Solution Explained in HINDI (2 Approaches)
https://youtu.be/hiNXVx6wm1s\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote 
https://youtu.be/hiNXVx6wm1s\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\nSubscribe link:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\n\nSubscribe Goal:- 400\nCurrent Subscriber:- 301\n\n# Approach 1 \n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        \n        int circleCount = 0;\n        int squareCount = 0;\n\n        for(int stud : students) {\n            if(stud == 0) {\n                circleCount++;\n            }\n            else {\n                squareCount++;\n            }\n        }\n\n        for(int sandwich : sandwiches) {\n\n            if(sandwich == 0 && circleCount == 0) {\n                return squareCount;\n            }\n            if(sandwich == 1 && squareCount == 0) {\n                return circleCount;\n            }\n            if(sandwich == 0) {\n                circleCount--;\n            }\n            else {\n                squareCount--;\n            }\n        }\n        return 0;\n    }\n}\n```\n# Approach 2\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n```\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        \n        int len = students.length;\n        ArrayDeque<Integer> studentQueue = new ArrayDeque<>();\n        ArrayDeque<Integer> sandwichStack = new ArrayDeque<>();\n\n        for(int i = 0; i < len; i++) {\n            sandwichStack.push(sandwiches[len - i - 1]);\n            studentQueue.offer(students[i]);\n        }\n\n        int lastServed = 0;\n        while(studentQueue.size() > 0 && lastServed < studentQueue.size()) {\n            if(sandwichStack.peek() == studentQueue.peek()) {\n                sandwichStack.pop();\n                studentQueue.poll();\n                lastServed = 0;\n            }\n            else {\n                studentQueue.offer(studentQueue.poll());\n                lastServed++;\n            }\n        }\n        return studentQueue.size();\n    }\n}\n```\n
['Java']