C++ solution with well defined comments
\nclass Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& a) {\n        int n = a.size();\n        \n        // To store index of zeros\n        set<
```\nclass Solution {\npublic:\n    vector<int> avoidFlood(vector<int>& a) {\n        int n = a.size();\n        \n        // To store index of zeros\n        set<int> index; \n        \n        // To store last index\n        unordered_map<int, int> last;\n        \n        // To store result\n        vector<int> res(n); \n        \n        for (int i=0; i<n; i++) {\n            if (a[i] == 0) {\n                \n                // store index of zeros\n                index.insert(i); \n                res[i] = 1;\n            } else {\n                res[i] = -1;\n                \n                // if it appeared already\n                if (last.find(a[i]) != last.end()) {\n                    \n                    // find a zero after last occurence\n                    auto it = index.upper_bound(last[a[i]]);\n                    \n                    // if couldn\'t find any\n                    if (it == index.end()) \n                        return {};\n                    \n                    // use a zero index to empty\n                    res[*it] = a[i];\n                    \n                    // remove from set \n                    index.erase(it);\n                }\n                \n                // update last index\n                last[a[i]] = i;\n            }\n        }\n        \n        return res;\n    }\n};\n```
[]