fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int[] a = new int[n];
  15. for(int i=0; i<n; i++){
  16. a[i] = sc.nextInt();
  17. }
  18. //solver
  19. int lo = -1, hi = n;
  20.  
  21. while(lo + 1<hi){
  22. int mid = lo + ( hi -lo ) /2;
  23. if(a[mid]==0){
  24. lo = mid;
  25. } else {
  26. hi = mid;
  27. }
  28. }
  29. int last0 = (lo==-1 || a[lo]!=0) ? -1 : lo;
  30. int first1 = (hi==n || a[hi]!=1) ? -1 : hi;
  31. System.out.println(last0 + "----" + first1);
  32. }
  33. }
Success #stdin #stdout 0.14s 58664KB
stdin
4
0 0 0 1
stdout
2----3