[C++] O(n) simple solution with comments
\nclass RLEIterator {\npublic:\n    \n    vector<int> enc; // the encoding vector\n    int curr_val = -1; // the current value in the encoding vector that we po
```\nclass RLEIterator {\npublic:\n    \n    vector<int> enc; // the encoding vector\n    int curr_val = -1; // the current value in the encoding vector that we point to\n    int curr_count = 0; // the current count of curr_val\n    int enc_pointer = 0; // the position in encoding vector that we currently point to\n    \n    RLEIterator(vector<int>& encoding) {\n        enc = encoding;     \n    }\n    \n    int next(int n) {\n    \n        for(int i = 0; i < n; ){\n\t\t\t\n\t\t\t// if curr_count is 0, get the next element in the encoding vector\n            if(curr_count == 0){\n                if(enc_pointer >= enc.size()) return -1;\n                curr_count = enc[enc_pointer++];\n                curr_val = enc[enc_pointer++];\n            }\n\t\t\t\n\t\t\t// if curr_count is greater than the amount that remains to be exhausted,\n\t\t\t// we can decrement curr_count and then return the current value\n            if(n - i <= curr_count){\n                curr_count -= n - i;\n                return curr_val;\n            }\n            \n\t\t\t// otherwise, we increment i be the amount that is possible to be exhausted and set curr_count to 0 \n            i += curr_count;\n            curr_count = 0; // we set curr_count to 0 because we have exhausted all remaining elements of value curr_val\n        }\n        return -1;\n        \n    }\n};\n```
['C', 'C++']