Dsa
Binary Search
Easy
Index of First Occurrence
You are given a sorted array that may contain duplicates and a target value. Your task is to find the index of the first occurrence of the target value in the array using binary search methods.
Input Format:
- The first line contains an integer n (1 <= n <= 10000).
- The second line contains n space-separated integers.
- The third line contains the target integer.
Output Format:
- Print the index of the first occurrence of the target value, or -1 if it does not exist.
Example:
Input:
7
1 2 2 2 3 4 5
2
Output:
1
Key concepts
binary_searcharraysearching
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now