int search(vector<int>& arr, int k) {
int low = 0, high = arr.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if the middle element is the target
if (arr[mid] == k) {
return mid;
}
// Determine which part is sorted
if (arr[low] <= arr[mid]) { // Left part is sorted
if (arr[low] <= k && k < arr[mid]) {
high = mid - 1; // Target is in the left sorted part
}
else {
low = mid + 1; // Target is in the right part
}
}
else { // Right part is sorted
if (arr[mid] < k && k <= arr[high]) {
low = mid + 1; // Target is in the right sorted part
}
else {
high = mid - 1; // Target is in the left part
}
}
}
return -1; // If the element is not found
}