title: Binary Search
signature: function binary_search(arr, l, r, x)
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;
  }
question_info.entry_fn_name: binary_search
question_info.entry_cls_name: Solution
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(arr, l, r, x, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, l, r, x) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({3, 4, 4, 8, 9, 13, 13, 15, 18, 27, 30, 32, 42, 48, 50, 52, 56, 66, 69, 69, 77, 84, 84, 93}, 19, 12, 22, -1)
      print(string.format("TEST-0...%s",result))
  
      result = driver({52, -58, -22, -80, 44, -52, -34, 94, -34, -74, 42, 60, -62, 70, 98, 32, 10, 94, 26, 56, -48, -50, 42, 2, 46, 28, -68, -16, -96, -12, 66, -46, 74, -60, -52, 28, -92, -78, 32, 28, 16, 34, 30, -60, -14}, 40, 35, 44, -1)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 1}, 1, 1, 1, 1)
      print(string.format("TEST-2...%s",result))
  
      result = driver({28, 84, 40, 81}, 2, 2, 2, -1)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-66, -62, -60, -56, -56, -2, 40, 44, 50, 74, 82, 94}, 8, 6, 8, -1)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1}, 7, 7, 10, -1)
      print(string.format("TEST-5...%s",result))
  
      result = driver({15, 26, 31, 36, 36, 61, 68, 72, 75, 79, 82, 98}, 6, 7, 8, -1)
      print(string.format("TEST-6...%s",result))
  
      result = driver({0, -82, -94, 48, 48, -96, 14, 66, 76, -30, 86, 28, -28, -66, -64, 92, -94, -66, 86, 26, 8, 94, -82, -80, 4, -26, 76, -46, 72, 88, -6, 8, -30, 40, -88, 2, -40, -98, -22, -20, 4, -12, 54, -20, -36, 12}, 38, 33, 39, -1)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, 12, 10, 6, -1)
      print(string.format("TEST-8...%s",result))
  
      result = driver({81, 47}, 1, 1, 1, -1)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.test_list: [{"idx": 0, "outputs": -1, "inputs": {"arr": [3, 4, 4, 8, 9, 13, 13, 15, 18, 27, 30, 32, 42, 48, 50, 52, 56, 66, 69, 69, 77, 84, 84, 93], "l": 19, "r": 12, "x": 22}}, {"idx": 1, "outputs": -1, "inputs": {"arr": [52, -58, -22, -80, 44, -52, -34, 94, -34, -74, 42, 60, -62, 70, 98, 32, 10, 94, 26, 56, -48, -50, 42, 2, 46, 28, -68, -16, -96, -12, 66, -46, 74, -60, -52, 28, -92, -78, 32, 28, 16, 34, 30, -60, -14], "l": 40, "r": 35, "x": 44}}, {"idx": 2, "outputs": 1, "inputs": {"arr": [0, 1], "l": 1, "r": 1, "x": 1}}, {"idx": 3, "outputs": -1, "inputs": {"arr": [28, 84, 40, 81], "l": 2, "r": 2, "x": 2}}, {"idx": 4, "outputs": -1, "inputs": {"arr": [-66, -62, -60, -56, -56, -2, 40, 44, 50, 74, 82, 94], "l": 8, "r": 6, "x": 8}}, {"idx": 5, "outputs": -1, "inputs": {"arr": [1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1], "l": 7, "r": 7, "x": 10}}, {"idx": 6, "outputs": -1, "inputs": {"arr": [15, 26, 31, 36, 36, 61, 68, 72, 75, 79, 82, 98], "l": 6, "r": 7, "x": 8}}, {"idx": 7, "outputs": -1, "inputs": {"arr": [0, -82, -94, 48, 48, -96, 14, 66, 76, -30, 86, 28, -28, -66, -64, 92, -94, -66, 86, 26, 8, 94, -82, -80, 4, -26, 76, -46, 72, 88, -6, 8, -30, 40, -88, 2, -40, -98, -22, -20, 4, -12, 54, -20, -36, 12], "l": 38, "r": 33, "x": 39}}, {"idx": 8, "outputs": -1, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], "l": 12, "r": 10, "x": 6}}, {"idx": 9, "outputs": -1, "inputs": {"arr": [81, 47], "l": 1, "r": 1, "x": 1}}]