Junior — Senior
Determining the position of the first occurrence of a key in a sorted array
livecode
Task condition
Given a sorted array data in ascending order. Implement a binary search algorithm that returns the index of the first occurrence of the specified value target. If the element is not found, the function should return -1.
int first(const std::vector<int> arr, int key) {
// implement
}
Examples of the function's operation:
data = {1, 2, 2, 2, 3},target = 2→ result1data = {1, 3, 5, 7},target = 4→ result-1