C++ || Easy || Global pointer
We just need a global pointer to store the last index till which the elements have been printed or sent to the output stream. The only way to do it (approach th
We just need a **global pointer** to store the **last index** till which the elements have been printed or sent to the output stream. The only way to do it (approach the problem) is in a linear fashion, from the starting index. Hence, the logic of the solution.\n**Please upvote if you find the solution good.\n*Thank You!!***\n```\nclass OrderedStream {\npublic:\n    int lastval = -1, n;\n    vector<string> v;\n    OrderedStream(int n) {\n        v = vector<string>(n);\n        this->n = n;\n    }\n    \n    vector<string> insert(int idKey, string value) {\n        v[idKey-1] = value;\n        if(idKey - 1 - lastval == 1) {\n            int idx = idKey-1;\n            vector<string> temp;\n            while(idx < n and v[idx].size()) temp.push_back(v[idx++]);\n            lastval = idx-1;\n            return temp;\n        }\n        return {};\n    }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n ```
['C', 'C++']