Count Occurrences of X in Sorted Array
Given a sorted array or list of integers with size N and an integer X, you need to determine how many times X appears in the array/list.
Input:
The first line of the input provides two integers, N and X, indicating the size of the array/list and the value of X.
The second line contains N space-separated integers representing the elements of the array/list.
Output:
Output a single integer that represents the total number of occurrences of X in the array/list.
Example:
Input:
N = 7
X = 3
array = [1, 2, 3, 3, 3, 4, 5]
Output:
3
Explanation:
X appears 3 times in the given array.
Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 109
- Arr[i] is the i-th element in the array/list.
- Time Limit: 1 second

AnswerBot
4mo
Count occurrences of a given integer in a sorted array.
Use binary search to find the first and last occurrence of X in the array.
Calculate the count by subtracting the indices of the last and first oc...read more
Help your peers!
Add answer anonymously...
Comviva Technology Software Developer interview questions & answers
A Software Developer was asked 9mo agoQ. Explain the concepts of OOP.
A Software Developer was asked Q. Pillers of oops , explain data science, concepts of ACID
A Software Developer was asked Q. Count Occurrences of X in Sorted Array Given a sorted array or list of integers ...read more
Popular interview questions of Software Developer
A Software Developer was asked 9mo agoQ1. Explain the concepts of OOP.
A Software Developer was asked Q2. Pillers of oops , explain data science, concepts of ACID
A Software Developer was asked Q3. Count Occurrences of X in Sorted Array Given a sorted array or list of integers ...read more
>
Comviva Technology 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

