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. //Finding upperbound for a array element
  14. int k = 40;
  15. int arr[] = {1,3,5,10,20,41,55,67,89};
  16. int n = arr.length;
  17.  
  18. int l = 0;
  19. int r = n-1;
  20. int upperbound =-1;
  21. while(l<=r){
  22. int mid = (l+r)/2;
  23. if(arr[mid] > k){
  24. upperbound = mid;
  25. r = mid-1;
  26. }
  27. else {
  28. l =mid+1;
  29. }
  30. }
  31. System.out.println(upperbound);
  32. }
  33. }
Success #stdin #stdout 0.1s 52516KB
stdin
Standard input is empty
stdout
5