Dsa
Binary Search
Easy
Book Ratings Search
You are given a sorted list of integers representing the ratings of books. You want to quickly check if a particular rating exists within this list. If the rating exists, return its index; if not, return -1.
Input format:
- The first line contains an integer n (1 <= n <= 10^5), the number of books.
- The second line contains n space-separated integers a_i (1 <= a_i <= 10^9) representing the ratings.
- The third line contains an integer q (1 <= q <= 10000), the number of queries.
- The next q lines each contain one integer x representing a rating to search for.
Output format:
- For each query, print the index of the rating in the list if it exists, or -1 if it does not.
Example:
Input:
5
3 5 7 9 12
3
5
10
3
Output:
1
-1
0
Key concepts
binary_searchsearcharray
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