First and Last Position of Element Sorted Array (LeetCode 34) | Full Solution | Study Algorithms
Vložit
- čas přidán 27. 07. 2024
- You are gIven an array of integers that is sorted in an ascending order and a target value. Find the index of first and last occurrence of the element in a fastest way possible. The desired complexity is less than linear time. Watch the video to learn how we can derive a clever solution using the concept of binary search. Work along with me to see how it can be solved easily along with the dry-run of code in JAVA.
Chapters:
00:00 - Intro
01:21 - Problem statement and description
03:12 - Brute Force Solution
06:03 - Efficient approach
07:44 - Using Binary Search Twice
10:30 - Dry-run of Code
14:26 - Final Thoughts
📚 Links to topics I talk about in the video:
Binary Search: • Binary Search Algorith...
Divide and Conquer: • Divide and Conquer alg...
Time Complexity: • What is the Time Compl...
What is Big O?: • Big O Notation Simplif...
LeetCode Problems: • Leetcode Solutions
📘 A text based explanation is available at: studyalgorithms.com/
Problem on LeetCode: leetcode.com/problems/find-fi...
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
I watched two explanations before this but couldn’t understand them. However, your teaching method was awesome, and I was able to write the code on my own right after understanding your solution. Thank you!
This is really one of the most underrated video on CZcams. The way you have explained this concept is awesome. Please do continue this good work. Everyone can be a good programmer/coder but everyone can't be a good teacher.
Thanks for all the love and support
I just started learning DSA when i am unable to solve leet code problem i first search in your youtube channel for a solution, In my opinion you are best at explain algorithms, Keep on going .
Great Explanation👏👏👏
The way of your explanation excellent 👏👏👏
u explain everything and it was quite easy to understand, thankyouuu
The way you make things clear is superr
the way of teaching makes us to solve the more problems
love it
Fantastic and clear explanation. Thank you!
You're very welcome!
Thank you sir ❤
brilliant🤩
Will this solution work if input is [1,2,3] and target is 2
What happens if the first and the last index both exists in left bound? or right bound? this approach will return last index or first index as -1 right?
Let’s say both first and last exist in the left bound.
When you first divide the array, you will not find the element in the middle, so following a binary search algorithm you will look in the left half. This will be your new array now.
So you will again find the first and last index in the left half.
Try out a test case on your own and you will understand it better :)
@@nikoo28 Kind of recursive
Yes indeed…this is a recursive solution. Just like binary search.
@@nikoo28 thank you
@@nikoo28 when low < high breaks the loop and the low value = 3 but the index value is still 2 how will you get 3 as output plz explain sir
just love the explanation