Can't debugg this !

Revision en2, by fares, 2016-01-31 10:10:17

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");
}

}~~~~~ edit:sorry for the non structural code(i can't organize the first 5 lines of it).

Tags help, binary search

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English fares 2016-01-31 23:32:19 38 Tiny change: '\n \n}\n\n~~~~~\n\' -
en4 English fares 2016-01-31 23:29:59 736 Tiny change: 'he code:\n~~~~~\n\' -
en3 English fares 2016-01-31 11:02:18 44 Tiny change: 'f it). \n\n ' -> 'f it). \nedit2:the code gives WA on huge test cases .\n '
en2 English fares 2016-01-31 10:10:17 106 Tiny change: 'de:\n~~~~~\n import ' -
en1 English fares 2016-01-31 09:44:58 1701 Initial revision (published)