leet code 3259 - Replace Elements with Greatest Element on Right Side C++ time(beats 87.79%), space(beats 22.06%) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28class Solution { public: vector<int> replaceElements(vector<int>& arr) { int arrSize = arr.size(); int max = 0; int tmp = 0; for(int i=arrSize-1; i>=0; --i) { if(i == arrSize-1) { max = arr[i]; arr[i] = -1; }else { if(arr[i] <= max) { arr[i] = max; }else { tmp = arr[i]; arr[i] = max; max = tmp; } } } return arr; } };