public class FirstOccurrenceFinder {
public static int solve(int n, int key, int[] v) {
int start = 0;
int end = n - 1;
int res = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (v[mid] == key) {
res = mid; // Record the index
end = mid - 1; // Move left to check if it occurs earlier
} else if (key < v[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {5, 10, 10, 10, 20, 20};
int key = 10;
int result = solve(arr.length, key, arr);
System.out.println("First Occurrence Index: " + result);
}
}