Leetcode problem: In-place Operations, help required!

Hi all, I am solving array practice problem in the topic in-place operation. The problem is to Replace element with greatest element on right side
for the given input in qus [17,18,5,4,6,1] i shoul get the output as [18,6,6,6,1,-1] but I am getting [18,6,6,6,6,-1]. And there some constraints to solve this qus like i need to modify the given array only and I can'take a new array and i need to replace last element of the array with -1. I am close to getting the output but not getting where I am going wrong. It will be good if any one from all of you can help. I am sharing my code here.

class Solution {
    public int[] replaceElements(int[] arr) {
        int max=0;
        for(int i=0;i<arr.length-1;i++){
            if(arr[i]<arr[i+1]){
                arr[i]=arr[i+1];
            }
            else if(arr[i]>arr[i+1]){
               max=arr[i+1];
                for(  int j=i+1;j<arr.length-1;j++){
                      
                     if(arr[j+1]>=max)
                     {
                        max=arr[j+1];
                        arr[i]=arr[j+1];
            
                    }
                }
                arr[arr.length-1]=-1;
            }
        }
       
   
        return arr;
}
}```
Comments (1)