Dsa
Binary Search
Medium
Finding a First Occurrence
Given a sorted array of integers 'nums' and an integer 'target', return the index of the first occurrence of 'target' in 'nums'. If 'target' is not present, return -1.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 10^5), the number of elements in the array.
- The second line contains n space-separated integers, the elements of the array (0 ≤ nums[i] ≤ 10^9).
- The third line contains a single integer target (0 ≤ target ≤ 10^9).
Output format:
- Print a single integer, the index of the first occurrence of target or -1 if not found.
Example:
Input:
5
1 2 2 2 3
2
Output:
1
Key concepts
binary_searcharraysearch
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