Simple C++ solution O(n) time
Instead of expanding the array, keep the index in the vector which will point to the current element  from which we have to start exhausting the elements.\n\n\n
Instead of expanding the array, keep the index in the vector which will point to the current element  from which we have to start exhausting the elements.\n\n```\nclass RLEIterator {\n    vector<int> values;\n    int index = 0, size;\npublic:\n    RLEIterator(vector<int> A) {\n        values = A;\n        size = values.size();\n    }\n    \n    int next(int n) {\n        if (n > 0) {\n            while (index < size) {\n                if (values[index] == 0) {  //If count = 0, then no need to check that element\n                    index += 2;\n                } else if (values[index] < n) {\n                    n = n - values[index];\n                    index += 2;\n                } else if (values[index] >= n) {\n                    values[index] = values[index] - n;\n                    return values[index + 1];\n                }\n            }\n        }\n        return -1;\n    }\n};\n```
[]