[C++] 2 Pointers vs 2 Passes Solution Compared and Explained, 100% Time, 80% Space
I wanted to solve this problem without actually using queues, since I feared they would be more wasteful in most cases, so I had to proceed with something else 
I wanted to solve this problem without actually using queues, since I feared they would be more wasteful in most cases, so I had to proceed with something else and I opted to just use the base inputs, helped by 2 pointers.\n\nTo do so, we will create a number of support variables, all integers:\n* `sandPos` and `studPos` were my 2 pointers to be moved across the inputs, both initialised to `0`;\n* `initStudPos` was a helper variable we might have to use later, to detect "cycles" (not sure if it is the right term, uh \uD83E\uDD14 ) in the queueing process;\n* `res` and `len` are both initialised to be equal the the lenght of our input, with the former decreasing everytime we serve a student successfully.\n\nWe will then proceed to loop until `res` is not `0` - ie: as long as we have student to serve.\n\nAt each iteration, we will either:\n* have the right student for the right sandwich, in which case we\n\t* decrease `res` by `1`;\n\t* mark that student as served;\n\t* increase both `studPos` and `sandPos` by `1`, remembering that the former will have to be adjusted with the modulo operator, since we are in a (potentially) circular queue, so once we reach the end, we have to restart from the first and so on;\n* have the wrong student for the right sanwich, n which case we:\n\t* set `initStudpos` to the current value of `studPos`\n\t* increase `studPos` by `1` with a modular correction, as described above;\n\t* keep increasing it even further, as long we either don\'t find a student matching the current sandwich on top of the stack or go for a full cycle;\n\t* if the former is the case, we `break` out of the loop.\n\nOnce we are done, we return `res` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    int countStudents(vector<int>& studs, vector<int>& sands) {\n        // support variables\n        int sandPos = 0, studPos = 0, initStudPos, res = studs.size(), len = studs.size();\n        while (res) {\n            // base case: the right sandwich for the right studen\n            if (studs[studPos] == sands[sandPos]) {\n                // updating res\n                res--;\n                // marking student as served\n                studs[studPos] = -1;\n                // advancing our pointers\n                studPos = (studPos + 1) % len;\n                sandPos++;\n            }\n            // otherwise, we need to move the queue until we either find a match or have done a full cycle\n            else {\n                // preparing the ground for the search of the next serviceable students\n                initStudPos = studPos;\n                studPos = (studPos + 1) % len;\n                // updating the queue as much as needed\n                while (studs[studPos] != sands[sandPos] && studPos != initStudPos) studPos = (studPos + 1) % len;\n                // exiting in case of a full circle without a match\n                if (studPos == initStudPos) break;\n            }\n        }\n        return res;\n    }\n};\n```\n\nThen it occurred to me I might have just taken another route: counting all the sandwich demand initially with `counter` and then move linearly through `sands` to see how many I might have still served.\n\nTo do so we will proceed in part similarly to the previous one, but only with the `sandPos` pointer and then will create a new variable, `counter`, that we populate soon after with the ongoing count of the sandwiches of each kind.\n\nIn the second loop, we will run through each sandwich `n` sequentially and:\n* if we still have demand for it, increase `sandPos` and decrease `counter[n]`, both by `1` tick;\n* if we run out of students interested in it, then we are in stalemate - no matter how much we want to rotate the queue, so we can just `break` out of the loop.\n\nFinally, we will return in any case `len - sandPos` (initial amount of available sandwiches minus the number we managed to give away) and we are done :)\n\nThe code, that seems to perform only marginally better:\n\n```cpp\nclass Solution {\npublic:\n    int countStudents(vector<int>& studs, vector<int>& sands) {\n        // support variables\n        int counter[2] = {}, len = studs.size(), sandPos = 0;\n        // populating counter\n        for (int n: studs) counter[n]++;\n        // main loop\n        for (int n: sands) {\n            // 1st case: there are still students demanding it\n            if (counter[n]) {\n                counter[n]--;\n                sandPos++;\n            }\n            // 2nd case: there are no more students demanding it\n            else break;\n        }\n        return len - sandPos;\n    }\n};\n```
['Two Pointers', 'Queue', 'C', 'C++']