Dsa
Binary Search
Medium
Bookstore Inventory
A bookstore's inventory is represented by an array of integers indicating the number of books in various categories. Given a target number of books, determine the index of the category that contains this exact number. If it doesn't exist, return the index where it could be inserted to maintain sorted order.
Input format:
- The first line contains space-separated integers representing the number of books in each category, sorted in ascending order.
- The second line contains a single integer representing the target number of books.
Output format:
- A single integer representing the index of the category where the target exists or could be inserted.
Example:
Input:
1 2 4 5 6
3
Output:
2
Key concepts
binary_searchsearch
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