Question: You are given a sorted array A of size n. Your task is to find the first occurrence and last occurrence of a given number

You are given a sorted array A of size n. Your task is to find the first occurrence and last occurrence of a given number x. Hint: Dont break your Search after finding the given number, keep on parsing the left and right sub-array until you find first and last occurrence of the given number.

3

Input: The first line of input contains the size of the array n and the given number x. Then it is followed by the array elements A1,A2,. . . ,An . Output: Print the first and last occurrence of the given number x. Print "X not Present" if the given number is not present in your array. Constraints: 1<=n<=105 0<=A[i]<=x<=105 NOTE: The indexing of the array starts with 0. Sample Input: 8 5 2 5 5 5 5 6 7 8 Sample Output: First occurrence of element 5 is at position: 1 Last occurrence of element 5 is at position: 4

in C++

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!