i think there is minor correction, we are using set not because we want all the index in sorted order but we want to erase that prev index of v.back() which is stored in mp[v.back()] in log(n) time. I might be wrong , but great explanation bhaiya ...