FastPrepCount Target Occurrences in a Sorted Array

Count Target Occurrences in a Sorted Array

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given a nondecreasing integer array nums and integer target, return how many times target occurs.

Your algorithm must run in O(log n) time.

Function

countTarget(nums: int[], target: int) → int

Examples

Example 1

nums = [1,2,2,2,3]target = 2return = 3

Target 2 occupies indices 1 through 3.

Example 2

nums = [1,3,5]target = 2return = 0

Target 2 is absent.

Constraints

  • 0 <= nums.length <= 10^5.
  • nums is sorted in nondecreasing order.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int countTarget(int[] nums, int target) {
  // Write your code here.
}
nums[1,2,2,2,3]
target2
expected3
Checking account…