title: Binary Search
signature: binary_search <- function(arr, l, r, x) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, l, r, x, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, l, r, x), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(3L, 4L, 4L, 8L, 9L, 13L, 13L, 15L, 18L, 27L, 30L, 32L, 42L, 48L, 50L, 52L, 56L, 66L, 69L, 69L, 77L, 84L, 84L, 93L), 19L, 12L, 22L, 
          -1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(52L, -58L, -22L, -80L, 44L, -52L, -34L, 94L, -34L, -74L, 42L, 60L, -62L, 70L, 98L, 32L, 10L, 94L, 26L, 56L, -48L, -50L, 42L, 2L, 46L, 28L, -68L, -16L, -96L, -12L, 66L, -46L, 74L, -60L, -52L, 28L, -92L, -78L, 32L, 28L, 16L, 34L, 30L, -60L, -14L), 40L, 35L, 44L, 
          -1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 1L), 1L, 1L, 1L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(28L, 84L, 40L, 81L), 2L, 2L, 2L, 
          -1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-66L, -62L, -60L, -56L, -56L, -2L, 40L, 44L, 50L, 74L, 82L, 94L), 8L, 6L, 8L, 
          -1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L), 7L, 7L, 10L, 
          -1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(15L, 26L, 31L, 36L, 36L, 61L, 68L, 72L, 75L, 79L, 82L, 98L), 6L, 7L, 8L, 
          -1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(0L, -82L, -94L, 48L, 48L, -96L, 14L, 66L, 76L, -30L, 86L, 28L, -28L, -66L, -64L, 92L, -94L, -66L, 86L, 26L, 8L, 94L, -82L, -80L, 4L, -26L, 76L, -46L, 72L, 88L, -6L, 8L, -30L, 40L, -88L, 2L, -40L, -98L, -22L, -20L, 4L, -12L, 54L, -20L, -36L, 12L), 38L, 33L, 39L, 
          -1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), 12L, 10L, 6L, 
          -1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(81L, 47L), 1L, 1L, 1L, 
          -1L
      )))
  
  }
  
  main()
source_py: def binary_search(arr, l, r, x):
      if (r >= l):
          mid = (l + ((r - l) // 2))
          if (arr[mid] == x):
              return mid
          elif (arr[mid] > x):
              return binary_search(arr, l, (mid - 1), x)
          else:
              return binary_search(arr, (mid + 1), r, x)
      else:
          return (- 1)
source_cpp: int binary_search(vector<int> arr, int l, int r, int x) {
    if ( r >= l ) {
      int mid = l + ( r - l ) / 2;
      if ( arr [ mid ] == x ) return mid;
      if ( arr [ mid ] > x ) return binary_search ( arr, l, mid - 1, x );
      return binary_search ( arr, mid + 1, r, x );
    }
    return - 1;
  }