Count NO2

Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.

Your algorithm's runtime complexity must be in the order of O(logn).

If the target is not found in the array, return[-1, -1].

Example 1:

Input:
 nums = [5,7,7,8,8,10], target = 8

Output: [3,4]

Example 2:

Input:
 nums = [5,7,7,8,8,10], target = 6

Output: [-1,-1]

Thoughts:

  1. Two binary searches: Search left as target - 0.5, right as target + 0.5

  2. Two binary searches:

  3. Divide and Conquer with early breaks

a variety of ways to solve the problem

Code: Solution 1: T O(logn)

Code: Solution 2: T O(logn)

C++

Python

Code: Divide and Conquer with early breaks

Last updated

Was this helpful?