Find First and Last Position of Element in Sorted Array

Find first and last position of element in sorted array.

Given a sorted array which contains duplicate elements. Write a code to find first and last position of a number in a sorted array.

OR

Find first and last occurrences of x in sorted array where x is a target value.

If a number is not found return -1.

For example –

Example 1 –

Input : {1, 4, 7, 8, 8, 11, 11, 11, 11, 12, 13, 13} , target = 11

Output: {5, 8}

The first index of 11 is 5 and last index is 8.

Example 2 –

Input: {1, 6, 7, 7, 8, 8, 9, 9},  target = 5

    Output: {-1, -1}

    The target value is not found.

NOTE – We have to solve this problem in O(logn) time complexity.

Before checking the solution, Let’s think for a moment how do you approach this problem? How we can efficiently find the first and last occurrences of x where x is the target element.

In this tutorial, i am going to discuss multiple approaches and their time and spaces complexities to solve this problem. At the end of this tutorial, I have shared the link to the video tutorial.

Programming video tutorials

Find first and last occurrences of x

Find First and Last Occurrences of an Element in a Sorted Array – Linear Search

The simplest approach is to traverse an array and find the indexes of first and last occurrences of x where x is a target number.

Here are the following steps –

i) Run a loop from i = 0 to n-1 where n is the size of an array.
ii) Declare two variables firstIndex and lastIndex. Initialized with -1 (firstIndex = -1 and lastIndex = -1 ).
iii) When target number is found first time, Assigned a new value to firstIndex.
iv) For last occurrence of a target keep assigning the value to lastIndex till the number is equal to the target number.

After the complete traversal, print the first and last index of a target number.

The time complexity of this approach is O(n).

This is the simplest approach to solve this problem. In this approach, we have not used the sorted property of an array.

How we can improve our solution to solve this problem in O(logn) time complexity.

We can solve this problem in O(logn) time complexity using binary search.
If you are not familiar with the binary search algorithm then check my previous tutorial on binary search algorithm.

Binary search in java

Binary search using recursion in java

Programming questions on binary search

Find First and Last Position of Element in Sorted Array using Binary Search

The array is sorted, so we can use binary search algorithm to find first and last position of a number in an array.

To solve this problem, We have to create two separate functions. The first function will find the first occurrence of an element in a sorted array. In second function we find the last occurrence of element in a sorted array.

The time complexity of this approach is O(logn).

Find First and Last Position of a Number in an Array using Binary Search : Video Tutorial

Find First and Last Position of Element in Sorted Array Video Tutorial

Tagged , , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.