Dsa
Binary Search
Hard
Book Page Finder
You are given an array of integers where each integer represents the number of pages in a book. Additionally, you are given a target page number. Your task is to find the first book that contains the target page number or determine that it is not present. Each book page is numbered from 1 to the total number of pages in that book. If the target page number is found, return the 1-based index of the book in the input array. If not found, return -1. The array is sorted in non-decreasing order.
Input format:
- The first line contains an integer n (1 <= n <= 10000): the number of books.
- The second line contains n integers (1 <= pages[i] <= 1000): the pages of each book sorted in non-decreasing order.
- The third line contains an integer target (1 <= target <= 10000): the target page number.
Output format:
- A single integer representing the 1-based index of the book containing the target page number or -1 if not found.
Example:
Input:
3
100 200 300
250
Output:
2
Key concepts
binary_searcharrayssearching
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