Beats 100% 🔥 || Cake Walk C++ Solution
Intuition\nThis problem requires us to determine how many students won\'t receive their preferred sandwich based on a given order of sandwiches and students\' p
# Intuition\nThis problem requires us to determine how many students won\'t receive their preferred sandwich based on a given order of sandwiches and students\' preferences.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count Preferences: Initially, we iterate through the students vector to count the number of students who prefer sandwich type 0 and sandwich type 1.\n2. Serve Sandwiches: Then, we iterate through the sandwiches vector. For each sandwich, we check if it matches the preference of the next student in line.\n   - If the sandwich matches the preference, we decrement the count of that preference, indicating that the student got their sandwich.\n   - If there are no more sandwiches of the required type, we return the count of remaining students who couldn\'t get a sandwich.\n\n**Code Run:**\n- We maintain two counters: countPreferZero for students preferring sandwich type 0, and countPreferOne for students preferring sandwich type 1.\n- We iterate through the students vector and increment the respective counters based on their preferences.\n- Then, we iterate through the sandwiches vector:\n  - If the sandwich type matches the preference of the next student, we decrement the respective counter.\n  - If the counter becomes negative, it indicates there are no more sandwiches of that type, and we return the count of remaining students who prefer the other type of sandwich.\n- If all students get their sandwiches, we return 0.\n\n# Complexity\n**Time complexity:**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- O(n), where n is the number of students or sandwiches. We iterate through the students and sandwiches once.\n\n**Space complexity:**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- O(1). We use a constant amount of extra space for variables regardless of the input size.\n\n# Code\n```\nclass Solution {\npublic:\n    int countStudents(vector<int>& students, vector<int>& sandwiches) {\n        int countPreferZero = 0; // Number of students preferring sandwich type 0\n        int countPreferOne = 0;  // Number of students preferring sandwich type 1\n        \n        // Count students\' sandwich preferences\n        for (int i = 0; i < students.size(); i++) {\n            if (students[i] == 1)\n                countPreferOne++;\n            else\n                countPreferZero++;\n        }\n        \n        // Serve sandwiches based on preferences\n        for (int i = 0; i < students.size(); i++) {\n            // Check if the sandwich matches the preference of the next student\n            if (sandwiches[i] == 0) {\n                // Serve sandwich type 0\n                countPreferZero--;\n                // Check if there are no more students who prefer sandwich type 0\n                if (countPreferZero < 0)\n                    return countPreferOne; // Return count of remaining students who prefer sandwich type 1\n            } else {\n                // Serve sandwich type 1\n                countPreferOne--;\n                // Check if there are no more students who prefer sandwich type 1\n                if (countPreferOne < 0)\n                    return countPreferZero; // Return count of remaining students who prefer sandwich type 0\n            }\n        }\n        \n        // All students got their sandwiches\n        return 0;\n    }\n};\n\n```
['Array', 'Simulation', 'C++']