import java.util.ArrayList;
public class Solution {
public static int search(ArrayList<Integer> arr, int n, int k) {
// Write your code here.
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if the element at mid is the target
if (arr.get(mid) == k) {
return mid;
}
// Check if the left half is sorted
if (arr.get(low)<= arr.get(mid)) {
// Check if 'k' is in the range of the left half
if (arr.get(low) <= k && k < arr.get(mid)) {
high = mid - 1; // Search the left half
} else {
low = mid + 1; // Search the right half
}
}
// Otherwise, the right half must be sorted
else {
// Check if 'k' is in the range of the right half
if (arr.get(mid) < k && k <= arr.get(high)) {
low = mid + 1; // Search the right half
} else {
high = mid - 1; // Search the left half
}
}
}
// If we reach here, 'k' is not in the array
return -1;
}
}