Java O(n) Solution O(1) space
\n//Pointer will move by 2\nclass RLEIterator {\n\n    int[] enc;\n    int pointer;\n    public RLEIterator(int[] encoding) {\n        this.enc = encoding;\n   
```\n//Pointer will move by 2\nclass RLEIterator {\n\n    int[] enc;\n    int pointer;\n    public RLEIterator(int[] encoding) {\n        this.enc = encoding;\n        this.pointer = 0;\n    }\n    \n    public int next(int n) {\n        int lastIndex = -1;\n        while (n > 0 && pointer < enc.length) {\n            \n            if (n <= enc[pointer]){ \n                enc[pointer] = enc[pointer] - n;    \n                n = 0;\n                lastIndex = enc[pointer+1]; \n            }\n            else {\n                n = n - enc[pointer];      \n                enc[pointer] = 0;\n                pointer = pointer +2;      \n            }\n        }\n        return lastIndex; \n    }\n}\n```
[]