DEV Community

Ganga Sri V
Ganga Sri V

Posted on

First and Last Occurrences

Problem Statement:
Given a sorted array arr with possibly some duplicates, the task is to find the first and last occurrences of an element x in the given array.
Note: If the number x is not found in the array then return both the indices as -1.

My Approach:
1.To find the first Occurance I search the required element in the beginning of the array if I find the element then consider the index for first and Last Occurance.
2.If not then I return as -1
3.for finding last element I search for the element in the reverse order of ther array if the first element of array is not the last element of the array.

Methods Used:
Linear Search
Conditional Check

Top comments (0)