Counting Approach ✅ | 99.85%🔥| DRY Run 🎯| Simple Easy to understand 💯Explained | Queue | Array
Code\njavascript []\nvar countStudents = function(students, sandwiches) {\n    while (students.length > 0 && students.indexOf(sandwiches[0]) !== -1) {\n        
# Code\n```javascript []\nvar countStudents = function(students, sandwiches) {\n    while (students.length > 0 && students.indexOf(sandwiches[0]) !== -1) {\n        if (students[0] === sandwiches[0]) {\n            students.shift();\n            sandwiches.shift();\n        } else {\n            students.push(students.shift());\n        }\n    }\n    return students.length;\n};\n\n```\n```python []\ndef countStudents(students, sandwiches):\n    while students and sandwiches[0] in students:\n        if students[0] == sandwiches[0]:\n            students.pop(0)\n            sandwiches.pop(0)\n        else:\n            students.append(students.pop(0))\n    return len(students)\n```\n```java []\nclass Solution {\n    public int countStudents(int[] students, int[] sandwiches) {\n        int n = students.length;\n        int m = sandwiches.length;\n        while (n > 0 && contains(students, sandwiches[0])) {\n            if (students[0] == sandwiches[0]) {\n                removeFirstElement(students);\n                removeFirstElement(sandwiches);\n            } else {\n                students = append(students, students[0]);\n                removeFirstElement(students);\n            }\n            n--;\n        }\n        return n;\n    }\n\n    private boolean contains(int[] arr, int target) {\n        for (int i : arr) {\n            if (i == target) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    private void removeFirstElement(int[] arr) {\n        for (int i = 0; i < arr.length - 1; i++) {\n            arr[i] = arr[i + 1];\n        }\n    }\n\n    private int[] append(int[] arr, int element) {\n        int[] newArr = new int[arr.length + 1];\n        for (int i = 0; i < arr.length; i++) {\n            newArr[i] = arr[i];\n        }\n        newArr[arr.length] = element;\n        return newArr;\n    }\n}\n```\n```cpp []\n#include <vector>\nusing namespace std;\n\nint countStudents(vector<int>& students, vector<int>& sandwiches) {\n    while (!students.empty() && find(students.begin(), students.end(), sandwiches[0]) != students.end()) {\n        if (students[0] == sandwiches[0]) {\n            students.erase(students.begin());\n            sandwiches.erase(sandwiches.begin());\n        } else {\n            students.push_back(students[0]);\n            students.erase(students.begin());\n        }\n    }\n    return students.size();\n}\n\n\n```\n```kotlin []\nfun countStudents(students: MutableList<Int>, sandwiches: MutableList<Int>): Int {\n    while (students.isNotEmpty() && students.contains(sandwiches[0])) {\n        if (students[0] == sandwiches[0]) {\n            students.removeAt(0)\n            sandwiches.removeAt(0)\n        } else {\n            students.add(students[0])\n            students.removeAt(0)\n        }\n    }\n    return students.size\n}\n\n```\n```ruby []\ndef count_students(students, sandwiches)\n    while !students.empty? && students.include?(sandwiches[0])\n        if students[0] == sandwiches[0]\n            students.shift\n            sandwiches.shift\n        else\n            students.push(students.shift)\n        end\n    end\n    students.length\nend\n\n```\n# \uD83D\uDD25DRY RUN \n\n| Iteration | Students       | Sandwiches      | Condition | Explanation                     |\n|-----------|----------------|-----------------|-----------|---------------------------------|\n| 0         | [1,1,1,0,0,1] | [1,0,0,0,1,1]   | true      | Sandwich found, shift both.    |\n| 1         | [1,1,0,0,0,1] | [0,0,0,1,1]     | true      | Sandwich found, shift both.    |\n| 2         | [1,0,0,0,1,1] | [0,0,1,1]       | true      | Sandwich found, shift both.    |\n| 3         | [0,0,0,1,1]   | [0,1,1]         | true      | Sandwich found, shift both.    |\n| 4         | [0,0,1,1]     | [1,1]           | true      | Sandwich found, shift both.    |\n| 5         | [0,1,1]       | [1]             | true      | Sandwich found, shift both.    |\n| 6         | [1,1]         | []              | true      | Sandwich found, shift both.    |\n| 7         | [1]           | []              | true      | Sandwich found, shift both.    |\n| 8         | []            | []              | false     | Loop ends, return students length. |\n\nOutput: 3\n\n\n# Prerequisite\uD83C\uDFAF\nFor this problem, we are using an array as a queue.\n\n## Array methods used inside problem :\n1. #### Pop():\n- The `pop()` method removes the *`last item of an array`*.\n- The `pop()` method changes the original array.\n- The `pop()` method returns the popped element.\n2. #### IndexOf():\n\n- The `indexOf()` method returns the position of the *`first occurrence of a value in an array`*.\n- The `indexOf()` method returns -1 if the value is not found.\n- The `indexOf()` method is used for searching an element in an array.\n3. #### Append():\n- The `append()` method adds an element at the end of the array.\n\n# \u2600\uFE0FApproach\uD83D\uDCAF\n1. While Loop:\n   - Continue the loop as long as there are students in the queue `(students.length > 0)` and the first sandwich in the queue is available among the students `(students.indexOf(sandwiches[0]) !== -1)`.\n   - This loop simulates serving sandwiches to students until there are no more sandwiches or no more students whose preferred sandwich is available.\n2. If Condition:\n   - If the first student\'s preferred sandwich matches the first sandwich in the queue, serve the sandwich to that student by removing them from both queues `(students.pop()` and `sandwiches.pop())`.\n   - If not, move the student to the end of the queue `(students.append(students.pop()))` to wait for their preferred sandwich.\n3. Return Length:\n   - After the loop ends, return the length of the students queue, which indicates the number of students who couldn\'t get their preferred sandwiches.\n\n\n# Complexity\n- Time complexity: `O(m) + O(n)` ~ `O(m + n)` ~ `O(n)`\n\n- Space complexity: `O(1)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n
['Array', 'Stack', 'Queue', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'Kotlin', 'JavaScript']