839. Count Occurrences in a Sorted Array

You are given a sorted array of integers arr and an integer target. Your task is to determine how many times target appears in arr.

Return the count of occurrences of target in the array.

Example 1:

Input: arr = [0, 0, 1, 1, 1, 2, 3], target = 1

Output: 3

Explanation: The number 1 appears 3 times in the array.

Example 2:

Input: arr = [5, 5, 5, 5, 5, 5], target = 5

Output: 6

Explanation: All elements in the array are 5, so the target appears 6 times.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [2, 4, 6, 8, 10], target = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= arr.length <= 106
  • 1 <= arr[i] <= 106
  • 1 <= target <= 106
0
class Solution {
public:
int countOccurrences(vector<int>& arr, int target) {
// Your code goes here
}
};
Test Case

Input:

Arr
Target