Frequency in a Sorted Array Problem Statement
Given a sorted array ARR
and a number X
, your task is to determine the count of occurrences of X
within ARR
.
Note:
- If
X
is not found in the array, return 0. - The array is sorted in non-decreasing order.
Input Format:
The first line contains an integer ‘T’ representing the number of test cases. Each test case includes two subsequent parts:
1. An integer N
representing the size of the input array.
2. A line of N
integers denoting the elements of the array.
3. A single integer X
for which you need to find occurrences.
Output Format:
For each test case, output a single line containing an integer representing the number of times the target element X
appears in the array.
Follow Up:
Try to implement a solution with O(log(N)) time complexity and O(1) space complexity.
Constraints:
- 1 ≤ T ≤ 102
- 1 ≤ N ≤ 104
- 0 ≤ ARR[i], X ≤ 109
- Time Limit: 1 sec
Special Note:
Your task is to implement the specified function, and you are not required to manage any input or output operations.

AnswerBot
4mo
Count occurrences of a number in a sorted array efficiently.
Use binary search to find the first and last occurrence of the target number in the array.
Calculate the count of occurrences by subtracting ...read more
Help your peers!
Add answer anonymously...
Urban Company Software Developer interview questions & answers
A Software Developer was asked 9mo agoQ. What is a thread?
A Software Developer was asked 10mo agoQ. You are given an array of prices where prices[i] is the price of a given stock o...read more
A Software Developer was asked 10mo agoQ. Given a sorted array of integers nums and an integer target, write a function th...read more
Popular interview questions of Software Developer
A Software Developer was asked 9mo agoQ1. What is a thread?
A Software Developer was asked 10mo agoQ2. You are given an array of prices where prices[i] is the price of a given stock o...read more
A Software Developer was asked 10mo agoQ3. Given a sorted array of integers nums and an integer target, write a function th...read more
>
Urban Company Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

