Simple nlogn solution in java

amazon
Tags: #<Tag:0x00007f182583a9c0>

#1
    public int[] subUnsort(int[] arr) {
    int n = arr.length;
    int brr[] = new int[n];
    for(int i=0;i<n;i++){
        brr[i] = arr[i];
    }
    Arrays.sort(brr);
    int i = 0, j=0; int fi=0, fj=0; int count = 0;
    while(i<n && j<n){
        if(arr[i] != brr[i]){
            count++;
            if(count == 1){
                fi = i;
            }
            fj = j;
            
            i++;
            j++;
        }else{
            i++;
            j++;
        }
    }
    
    if(fi == 0 && fj == 0){
        int res[] = {-1};
        return res;
    }
    int res[] = new int[2];
    res[0] = fi;
    res[1] = fj;
    return res;
    
}