#include <iostream>
#include <vector>
using namespace std;
int binarySearch(vector<int> [arr](https://www.google.com/search?q=arr), int [l](https://www.google.com/search?q=l), int [r](https://www.google.com/search?q=r), int [x](https://www.google.com/search?q=x)) {
if ([r](https://www.google.com/search?q=r) >= [l](https://www.google.com/search?q=l)) {
int mid = [l](https://www.google.com/search?q=l) + ([r](https://www.google.com/search?q=r) - [l](https://www.google.com/search?q=l)) / 2;
if ([arr](https://www.google.com/search?q=arr)[mid] == [x](https://www.google.com/search?q=x))
return mid;
if ([arr](https://www.google.com/search?q=arr)[mid] > [x](https://www.google.com/search?q=x))
return binarySearch([arr](https://www.google.com/search?q=arr), [l](https://www.google.com/search?q=l), mid - 1, [x](https://www.google.com/search?q=x));
return binarySearch([arr](https://www.google.com/search?q=arr), mid + 1, [r](https://www.google.com/search?q=r), [x](https://www.google.com/search?q=x));
}
return -1;
}
int main() {
vector<int> [arr](https://www.google.com/search?q=arr) = { 2, 3, 4, 10, 40 };
int [n](https://www.google.com/search?q=n) = [arr](https://www.google.com/search?q=arr).size();
int [x](https://www.google.com/search?q=x) = 10;
int result = binarySearch([arr](https://www.google.com/search?q=arr), 0, [n](https://www.google.com/search?q=n) - 1, [x](https://www.google.com/search?q=x));
if (result == -1)
cout << "Element is not present in array" << endl;
else
cout << "Element is present at index " << result << endl;
return 0;
}
this was the reponse I got when I asked it to code binary search in c++.