1class Solution {2public:3vector<int> replaceElements(vector<int> &arr) {4int n = arr.size();56// taking last index as greatest for now7int g = arr[n - 1];8// setting last index as -19arr[n - 1] = -1;10for (int i = n - 2; i >= 0; i--) {11// storing last index value to be changed to comapare with the greatest12// value till now13int h = arr[i];14// assigning greatest till now from right15arr[i] = g;16// checking if current is greater than the previous indices17if (h > g) {18g = h;19}20}21// returning the value22return arr;23}24};