Easiest and Clean C++ Sol | Beats 100% Users | Easiest Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\nI applied the very brute force approach seeing such small constraints.\nStored the Fo
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![8 april potd.png](https://assets.leetcode.com/users/images/c66bcbc0-0be9-4f0f-af86-41cf00ed592b_1712555025.1397405.png)\nI applied the very brute force approach seeing such small constraints.\nStored the `Food` array in stack and `Boys` array in a queue as mentioned in the question and then applied the required transformations.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Store `food` array in a stack `st` and sum its elements in `fsum`. Similarly, store `boys` array in a `deque` and sum its elements in `bsum`.\n2. `fsum` and `bsum` counts number of `1\'s` in their respective arrays, if they are equal all boys will eventually get their food, therefore return 0.\n3. Else Preform the following operation while true:\n    _-_`if(st.top()==q.front())` i.e. boy get the required food-\n        -----a)adjust `bsum`\n        -----b)Remove food and boy from the stack and queue\n    _-_`else` If boy do not get the required food-\n        -----a)Take the boy at front and push him to the queue end.\n        -----b)Check if queue is filled with all `1\'s`          (`bsum==q.size()`) or queue is filled with all `0\'s` (`bsum==0`). For this condition, no one will be able to get food, therefore break.\n\n4. At last return size of `q` which contains all left out boys without required food.\n\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# Please UPVOTE if you like it : )\n\n# Code\n```\n#include<bits/stdc++.h>\nclass Solution {\npublic:\n    int countStudents(vector<int>& boys, vector<int>& food) {\n        deque<int> q;\n        stack<int> st;\n        int bsum=0,fsum=0;\n        for(int i=food.size()-1;i>=0;i--)st.push(food[i]),fsum+=food[i];\n        for(int i=boys.size()-1;i>=0;i--)q.push_front(boys[i]),bsum+=boys[i];\n        if(bsum==fsum)return 0;\n        while(true){\n            if(st.top()==q.front()){ // boy get the required food\n                bsum-=q.front();\n                st.pop();\n                q.pop_front();\n            }\n            else{\n                q.push_back(q.front());\n                q.pop_front();\n                if(bsum==q.size() || bsum==0)break;\n            }  \n        }\n        return q.size();\n    }\n};\n```\n![THIS IS BILL.png](https://assets.leetcode.com/users/images/ef814e6d-6696-42b0-84a4-f584db353a0e_1712555051.8858678.png)\n
['C++']