title: Binary Search
signature: int binarySearch(vector<int> arr, int l, int r, int x) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<int> arr, int l, int r, int x, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, l, r, x), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      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); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      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); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 1}, 1, 1, 1, 1); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({28, 84, 40, 81}, 2, 2, 2, -1); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-66, -62, -60, -56, -56, -2, 40, 44, 50, 74, 82, 94}, 8, 6, 8, -1); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1}, 7, 7, 10, -1); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({15, 26, 31, 36, 36, 61, 68, 72, 75, 79, 82, 98}, 6, 7, 8, -1); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      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); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, 12, 10, 6, -1); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({81, 47}, 1, 1, 1, -1); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }