Hi: I was solving this problem http://codeforces.me/problemset/problem/451/B
My algorithm is to put the numbers in an array ,make a new array that have the same numbers, put it in sorted order ,loop on the unsorted one, compare each entry of the two arrays , if they are not equal binary search the entry of the unsorted array entry in the sorted on to get the index of it, Compare the entry using this index in the unsorted Array with the sorted first entry compared in the loop , If equal increment some variable and get the last and first index of the segment ,else do nothing, After that check if the variable equal 1 ,true:print yes with the index of the first and last element , or check if the array was sorted if so print yes with 1 1 , else print no.
Here is the code: import java.util.*; public class Main { public static void main (String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int unsort[]=new int[n]; int sort[]=new int[n]; for(int i=0;i<unsort.length;i++){ unsort[i]=sc.nextInt(); sort[i]=unsort[i]; } Arrays.sort(sort); int co=0;int counter=0,id=0,id2=1; for(int i=0;i<unsort.length;i++){ if(unsort[i]!=sort[i]){ co++; int in=Arrays.binarySearch(sort,unsort[i]); if(sort[i]==unsort[in]&&sort[i+1]==unsort[in-1]){ int i2=i;int in2=in;boolean b=false; while(i2++<=in2--){ if(sort[i2]!=unsort[in2]){b=true;break;}
}
if(b==false){
++counter;id=i;id2=in;
i=in;}
}
}
}
++id;++id2;
if(co==0){id=1;id2=1;}
System.out.println((counter==1||co==0)?"yes\n"+id+" "+id2:"no");
}
}



