Java Easy Understanding Deque solution
\nclass RLEIterator {\n    Deque<Pair<Integer,Integer>> deque = new ArrayDeque(); //key: num frequency, value: num value\n    \n    public RLEIterator(int[] enc
```\nclass RLEIterator {\n    Deque<Pair<Integer,Integer>> deque = new ArrayDeque(); //key: num frequency, value: num value\n    \n    public RLEIterator(int[] encoding) {\n            for(int i = 0 ; i + 1 < encoding.length; i= i+2){\n                if(encoding[i] != 0)\n                    deque.add(new Pair(encoding[i], encoding[i+1]));\n            }\n    }\n    \n    public int next(int n) {\n        Pair<Integer,Integer> item = deque.peek();\n        if(item == null)\n            return -1;\n        if(item.getKey() > n) //decrement the num frequency\n            decrementFrequency(item, n);\n        else if (item.getKey() == n) //just remove the item from queue\n            deque.poll();\n        else { // need to go and find another candidate to pass it also\n            while( item != null && n > item.getKey()){\n                n = n - item.getKey();\n                deque.poll();\n                item = deque.peek();\n            }\n            if(item == null)\n                return -1;\n            else\n                decrementFrequency(item, n);\n        }\n        return item.getValue();\n     }\n    \n    private void decrementFrequency(Pair<Integer,Integer> item,int n){\n        int key = item.getKey() - n;\n        deque.poll();\n        deque.addFirst(new Pair(key, item.getValue()));\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```
['Queue', 'Java']