Рет қаралды 8,794
This question has been asked in the interview of Facebook, Google, and Microsoft and is a Leetcode problem number 34.
The question reads - Given an array of integer numbers sorted in ascending order, find the starting and ending position of a given target value.
if the target is not found in the array return [-1,-1].
You must write an algorithm with O(log n) runtime complexity.
Now a simple brute force approach will be to run a loop from the first index and return the index when the element is found first for a first position or starting position.
And, for the ending or last position run a loop from the last index.
But in this method, we will compromise with the time complexity given.
So one thing which is very clear is that we have to use the binary search algorithm for this program.
Let's see how a binary search approach with little changes can give the desired results as asked in this program.
Subscribe to our channel for regular updates on the dsa course and click on the bell icon to never miss an update from our dsa course.
Data Structures and Algorithms Free Course (Learn DSA Without Paise) Playlist - • Data Structures And Al...
For more information, fill this form: forms.gle/8eiUmM92Fx563Aen9
or call us at 8884881203
Facebook: / thetapacademy
Instagram: / tapacademy_online
Linkedin: / 73820805
Website: www.thetapacademy.com
#coding #dsa #dsacourse #java #javainterview #array #binarysearch