Your question is Count Occurrences in Sorted Array. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Blinkit's catalog and fulfillment systems may store sorted item IDs with repeated values. Given a sorted array and a target item ID, return how many times the target occurs.
Your solution must use binary search rather than scanning every element. Find the first index where the target could appear and the first index after its final occurrence. Their difference is the required count.
Implement count_occurrences(nums, target).
nums, a list of integers sorted in non-decreasing order, and target, an integer.nums equal to target.0 when the target is absent.def count_occurrences(nums, target):