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
Be the first one to answer
Add answer anonymously...
Comviva Technology Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter