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. // your code goes here
  13.  
  14. Scanner sc = new Scanner(System.in);
  15. int t = sc.nextInt();
  16. while (t != 0) {
  17. int n =sc.nextInt();
  18. int array[]= new int[n];
  19. for (int i = 0; i <n ; i++) {
  20. array[i]=sc.nextInt();
  21. }
  22. int result = indexOfFirstOne(array, 0, n);
  23. System.out.println(result);
  24. t--;
  25. }
  26.  
  27. }
  28. public static int indexOfFirstOne(int arr[], int low, int high) {
  29. int mid=low+(high-low)/2;
  30. if (low>high || mid >= arr.length )
  31. return -1;
  32. if ( mid-1 >= 0 && arr[mid]==1 && arr[mid-1]==0) // CHNAGED
  33. return mid;
  34. else if( arr[mid] == 0 )
  35. return indexOfFirstOne(arr,mid+1,high);
  36. else if( arr[mid] == 1 )
  37. return indexOfFirstOne(arr,low, mid-1);// CHNAGED
  38. return -1 ;
  39. }
  40. }
Success #stdin #stdout 0.13s 49492KB
stdin
1
3
0 0 0 
stdout
-1