You have been given a sorted array/list of integers 'arr' of size 'n' and an integer 'x'.
Find the total number of occurrences of 'x' in the array/list.
Input: 'n' = 7, 'x' = 3
'arr' = [1, 1, 1, 2, 2, 3, 3]
Output: 2
Explanation: Total occurrences of '3' in the array 'arr' is 2.
The first line of the input contains two integers, n, and x. They represent the size of the array/list and x, respectively.
The second line contains n single space-separated integer representing the array/list elements.
Return the total number of occurrences of x in the array/list.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
7 3
1 1 1 2 2 3 3
2
In the given list, there are 2 occurrences of integer 3.
5 6
1 2 4 4 5
0
In the given list, there are 0 occurrences of integer 6.
The expected time complexity is O(log 'n').
1 <= n <= 10^4
1 <= arr[i] <= 10^9
1 <= x <= 10^9
Where arr[i] represents the element i-th element in the array/list.
Time Limit: 1sec
Visit every element in the array and keep a track of whether the element being visited is X or not.
O(N), Where N is the total number of elements in the array of the size of the array.
You visit every element once, making a total of N operations.
Therefore the time complexity is O(N).
O(1)
We are using constant extra space.
Therefore the space complexity is O(1).