Easy Java Solution TC: O(N), SC: O(1)
Intuition\n Describe your first thoughts on how to solve this problem. \nEven indexes show the frequency of the corresponding next number.\nWe can decrement thi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEven indexes show the frequency of the corresponding next number.\nWe can decrement this number to see if there is anything left to add with the next number.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nKeep a reference to the even indexes. In a loop till ```n``` is more than zero, decrement the frequency if it is more than n. Otherwise, decrement ```n``` by the difference. Don\'t forget to check the reference pointer going out of bound.\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass RLEIterator {\n    int pointer;\n    int[] encoded;\n\n    public RLEIterator(int[] encoding) {\n        pointer = 0;\n        encoded = Arrays.copyOf(encoding, encoding.length);\n    }\n    \n    public int next(int n) {\n        while(n > 0) {\n            // check out of bound condition\n            if (pointer >= encoded.length) return -1;\n\n            // Only two cases possible\n            // 1. Frequency is more than n -> Decrease the frequency by n\n            // 2. Frequency is less than n -> Decrease n by the difference\n            if (encoded[pointer] >= n) {\n                encoded[pointer] -= n;\n                return encoded[pointer + 1];\n            } else {\n                n = n - encoded[pointer];\n                // move the reference to the next frequency location\n                pointer += 2;\n            }\n        }\n\n        return encoded[pointer + 1];\n    }\n}\n\n\n/**\n * Your RLEIterator object will be instantiated and called as such:\n * RLEIterator obj = new RLEIterator(encoding);\n * int param_1 = obj.next(n);\n */\n```
['Java']