Count Occurrences in Sorted Array
Easyjavapythonccppjavascript
Given a sorted array and a target value, count how many times the target occurs.
Input Format
The first line contains two integers N and target. The second line contains N space-separated integers in non-decreasing order.
Output Format
Print the number of occurrences of target.
Example 1
Input
6 2 1 2 2 2 4 5
Output
3
Explanation: 2 occurs three times.
- 1 <= N <= 100000
- The array is sorted in non-decreasing order.
- -1000000000 <= A[i], target <= 1000000000
Submit an attempt to unlock the solution. (Sign in first.)
Related Problems
- Linear SearchEasy · java, python, c, cpp, javascriptSolve Problem
- Binary SearchEasy · java, python, c, cpp, javascriptSolve Problem
- First Occurrence in Sorted ArrayEasy · java, python, c, cpp, javascriptSolve Problem