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 int find_first(int[] arr, int n, int tg){
  11. int l=0;
  12. int r = n-1;
  13. int ans1 = -1;
  14. while(l<=r){
  15. int m = l + (r-l)/2;
  16. if(arr[m] == tg){
  17. ans1 = m;
  18. r=m-1;
  19. }
  20. else if(arr[m] > tg){
  21. r = m-1;
  22. }
  23. else{
  24. l = m+1;
  25. }
  26. }
  27. return ans1;
  28. }
  29.  
  30. public static int find_last(int[] arr, int n, int tg){
  31. int l=0;
  32. int r = n-1;
  33. int ans2 = -1;
  34. while(l<=r){
  35. int m = l + (r-l)/2;
  36. if(arr[m] == tg){
  37. ans2 = m;
  38. l=m+1;
  39. }
  40. else if(arr[m] > tg){
  41. r = m-1;
  42. }
  43. else{
  44. l = m+1;
  45. }
  46. }
  47. return ans2;
  48. }
  49. public static void main (String[] args) throws java.lang.Exception
  50. {
  51. // your code goes here
  52. Scanner sc = new Scanner(System.in);
  53. int n = sc.nextInt();
  54. int[] arr = new int[n];
  55. for(int i=0;i<n;i++){
  56. arr[i] = sc.nextInt();
  57. }
  58. int tg = sc.nextInt();
  59. int ans1 = find_first(arr,n,tg);
  60. int ans2 = find_last(arr,n,tg);
  61. System.out.println(ans1 + " " + ans2);
  62.  
  63. }
  64. }
Success #stdin #stdout 0.22s 60824KB
stdin
0

6
stdout
-1 -1