Occurrence of X in a Sorted Array Problem Statement
Given a sorted array of integers of size N and an integer X, determine the total number of occurrences of X in the array.
Input:
The first line of the input contains two integers N and X, representing the size of the array and the integer X, respectively.
The second line contains N space-separated integers representing the elements of the array.
Output:
Output a single integer representing the total number of times X occurs in the array.
Example:
Input:
5 2
1 2 2 2 3
Output:
3
Constraints:
1 <= N <= 105
1 <= Arr[i] <= 109
whereArr[i]
is the ith element in the array.- Time Limit: 1sec

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 total occurrences by subtracting the indices and adding 1...read more
Help your peers!
Add answer anonymously...
Directi Software Developer interview questions & answers
A Software Developer was asked Q. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more
Popular interview questions of Software Developer
A Software Developer was asked Q1. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q2. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q3. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more
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

