Dsa
Binary Search
Easy
Book Page Number Search
You have a list of book page numbers sorted in ascending order. Given a page number x, determine if it exists in the list and return the index of the page or -1 if it doesn't exist. The input will specify the number of pages followed by the sorted pages and finally, the page number to search for.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 1000).
- The second line contains n integers sorted in ascending order.
- The third line contains the integer x to search for.
Output format:
- Print the index of x or -1 if not found.
Example:
Input:
5
1 2 3 4 5
3
Output:
2
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