C++ solution with iterators, no need to store array
The following solution makes use of iterators , instead of having to save full array.\n\n\nclass RLEIterator {\npublic:\n    vector<int>::iterator curIndex, end
The following solution makes use of iterators , instead of having to save full array.\n\n```\nclass RLEIterator {\npublic:\n    vector<int>::iterator curIndex, end;\n    RLEIterator(vector<int>& A) {\n        curIndex = A.begin();\n        end = A.end();\n    }\n    \n    int next(int n) {\n        while (curIndex != end && *curIndex < n) {\n            n -= *curIndex;\n            curIndex += 2;\n        }\n        if (curIndex != end && *curIndex >= n) {\n            (*curIndex) -= n;\n            return *(curIndex+1);\n        }\n        return -1;\n    }\n};\n```
[]