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 Main
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int n = sc.nextInt();
  15. int[] arr = new int[n];
  16. for(int i=0;i<n;i++){
  17. arr[i] = sc.nextInt();
  18. }
  19.  
  20. int x = sc.nextInt();
  21.  
  22. int l = 0;
  23. int r = n-1;
  24. int ans = -1;
  25. while(l<=r){
  26. int m = l + (r-l)/2;
  27. if(arr[m] > x){
  28. ans = m;
  29. r=m-1;
  30. }
  31. else{
  32. l=m+1;
  33. }
  34. }
  35. System.out.println(ans);
  36.  
  37. }
  38. }
Success #stdin #stdout 0.14s 56560KB
stdin
5
1 2 3 8 10
12
stdout
-1