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. int[] array = {1, 2, 2, 3, 3, 3, 4, 5}; // Create an integer array of size 5
  14. int n = array.length;
  15. int target = 3;
  16. int l = 0, r = n - 1;
  17. int ans = -1;
  18. while(l<r){
  19. int mid = l+(r-l)/2;
  20. if(array[mid] < target){
  21. l = mid + 1;
  22. } else{
  23. r = mid;
  24. ans = r;
  25. }
  26. }
  27.  
  28. System.out.print(ans);
  29. }
  30. }
Success #stdin #stdout 0.08s 54504KB
stdin
Standard input is empty
stdout
3