fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone {
  7. public static void main (String[] args) throws java.lang.Exception {
  8. int a[] = {100, -23, -5, 0, 12, 19, 27, 36, 79, 100, 191}; // Corrected array initialization
  9. int K = 27;
  10. int m = binarySearch(a, K);
  11. System.out.println(m);
  12. }
  13.  
  14. public static int binarySearch(int a[], int K) {
  15. int low = 0, high = a.length - 1;
  16. while (low <= high) {
  17. int mid = low + (high - low) / 2;
  18. if (a[mid] == K) {
  19. return mid;
  20. } else if (a[mid] < K) {
  21. low = mid + 1;
  22. } else {
  23. high = mid - 1;
  24. }
  25. }
  26. return -1; // Return -1 when the element is not found
  27. }
  28. }
  29.  
Success #stdin #stdout 0.1s 54616KB
stdin
Standard input is empty
stdout
6