package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
 */
public class Problem1299 {
    public int[] replaceElements(int[] arr) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1299 prob = new Problem1299();
        System.out.println(Arrays.toString(prob.replaceElements(new int[]{17, 18, 5, 4, 6, 1}))); // [18,6,6,6,1,-1]
    }
}

package leetcode;

/**
 * https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
 */
public class Problem1299 {
    public int[] replaceElements(int[] arr) {
        int[] answer = new int[arr.length];
        answer[arr.length - 1] = -1;
        int max = arr[arr.length - 1];
        for (int i = arr.length - 2; i >= 0; i--) {
            answer[i] = max;
            max = Math.max(max, arr[i]);
        }
        return answer;
    }
}

Solve problem 1299