Dsa
Binary Search
Easy
Ice Cream Shop
You are at an ice cream shop that has a list of flavors sorted in order of popularity. Given a popularity score, your task is to find if that score is present in the list, and if so, return the index of the first occurrence. If the score is not present, return -1.
Input format:
- The first line contains an integer n (1 <= n <= 10^5), the number of flavors.
- The second line contains n space-separated integers representing the popularity scores.
- The third line contains an integer score representing the popularity score you want to find.
Output format:
- Print the index of the flavor with the specified popularity score, or -1 if it doesn't exist.
Example:
Input:
5
1 2 2 3 4
2
Output:
1
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