What will be the output of the following code snippet?
#include <iostream>
using namespace std;
int binarySearch(int arr[], int low, int high, int target) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 8;
int index = binarySearch(arr, 0, size - 1, target);
cout << "Element found at index: " << index;
return 0;
}
Correct answer is option 'C'. Can you explain this answer?