Easy Java Solution using Arrays.sort()


#1

Solution in O(nlog(n)) passes all test cases
public class Solution {
public ArrayList subUnsort(ArrayList A) {
int st=0;
int en=0;
int arr[]=new int[A.size()];
boolean flag=true;
for(int i=0;i<A.size();i++)
{
arr[i]=A.get(i);
}
Arrays.sort(arr);
for(int i=0;i<A.size();i++)
{
if(arr[i]!=A.get(i))
{
st=i;
flag=false;
break;
}

    }
    for(int i=A.size()-1;i>=0;i--)
    {
        if(arr[i]!=A.get(i))
        {
            en=i;
            break;
        }
    }
    
    ArrayList<Integer> out=new ArrayList<>();
    if(flag==false)
    {
    out.add(st);
    out.add(en);
    }
    else
    out.add(-1);
    return out;
}

}