Search in rotated sorted array | Leetcode #33
Vložit
- čas přidán 18. 04. 2020
- This video explains a very important interview coding problem which is to search a target element given a originally sorted array in ascending order which has now been rotated. We are required to find the pivot element in just O(log N) time. I have explained 2 binary search approaches. The first approach finds the target element by first calculating the pivot element and then find the target element which takes 2 traversals of the given array. The second approach is better which solves the problem in just a single traversal of the array using binary search and this is the most optimal approach for this problem. CODE for BOTH approaches are present in the same CODE LINK file. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: gist.github.com/SuryaPratapK/...
Dude, I appreciate the detailed presentation, you should know that you are doing a great service !
Thanks:)
Totally agreed bro!! he is doing really good job, thanks !!
This video is extremely illuminating. We just need more videos like this one. Leetcode discussion always talks about the final optimized solution where the thought process is invisible. This video demonstrates the thought process -> 2pass solution -> 1pass solution and finally helps everyone understand how the optimization occurs. Good job!
last moment rap was awesome :D
Even I liked it :P
😅
This was a great explanation! I've watched a couple of videos and read some explanations on Leetcode for this problem, but I still couldn't quite understand it. There were two things you said that really made it click for me:
1. There is always at least 1 half of the array where the values are increasing.
2. We want to find that half, and then see if the target is inside that subsection.
got an offer from Microsoft after learning from this channel. Thanks :)
i was really confused by the searching in the uniform approach at first but i realized that its easier to set the conditions for the uniform approach than the non uniform approach, thanks!
From the 7th Minute onwards, this video is GOLD !!!!! Jaha Pana Tussi Great Ho !!!!!!
BEST explanation I've seen on CZcams for this problem. Thank you!
Wow! I watched a lot of videos but I just could not wrap my head around it.. This explanation was the answer which I was looking for!!!!
thanks I was gettin TLE in the first approach which I had thought by myself. Ur one pass solution really helped me :)
This is best explanation from all other explanations I have seen/read for this problem.
A really good explanation. Could not imagine that this problem could be solved in this way as well! Appreciate the work.
Thanks
For dummies, your explaintion is always clear than ever. You deserve more subcribers. Thank you Tech Dose :)
Welcome :)
Very clear and precise explaination. Got it in the first go. Thanks
Excellent Explanation , haven't found better explanation of any problems than yours 🙏🏻
Looked at several other videos but still not clear. this one explains with great overall picture and logic ! thanks a lot !
Welcome 😀
one of the best application of binary search, you explained it very well, the approach without finding pivot element is best approach
Thanks :) Actually using this approach, many harder problems are also solved.
Hey, is it a good practise to generalise a pattern after seeing three examples? Is there any other way to prove there'll bs strictly increasing sequence in one half of the array?
Straightforward and clear! Thanks!
Very detailed and clear explanation, very thanks for all your effort
Sir, your channel deserves so much more attention and subs. Thank you.
Thanks ❤️
Great explanation! subscribed to the channel!... Keep up the good work
Thanks :)
Great video :) I was a little confused about the code for the first approach - why is the right pointer updated to mid rather than mid - 1?
great ! i loved your explanation ... keep up the good work !
One of the finest explaination, Thank You!
Great visual presentation loved it
Finally understood this problem!!
Thanks much!!
At 12:06 , line 23 can also we written without checking target equal to mid elements. .ie
line 23 : if(target
really appreciate your explanation skills.
It becomes so easy to understand the approach to solve problems by watching your videos. Thanks a lot for making it so easy for us to understand the solutions.
Welcome :)
Best explanation to the problem. Thanks for taking the time to make the video.
just amazing ! A huge appreciation for you.
Thanks :)
This explantation is greater than any others.
❤️ Thanks
gud work bro... the explanation was simple and examples were ample to be able to understand this easily :)
Thanks for the explanation. It really helps.
amazing explanation, thank you!
Really thanks buddy, Doing a great job :)
Best explanation ever. Thanks a lot.
Best content i have ever seen in youtube thankyou so much .we just need to know more nd more from you😊😊😊 ...
Thanks ❤️
Very clear explanation, thank you!
Welcome :)
Thanks! Very well explained. I have a question about line 21 as explained @ 11:40. You say we are checking for strictly increasing left half, but check a[left]
same ques
Awesome! Thank you!
Amazing explanation!
Sir, this code won't work for rotated sorted array (sorted in descending order) right, we should be changing it to (nums[left] >= nums[mid]), am I right sir ?
Thankyou for such a great explanation really great video!!!!!
Thanks :)
Very Helpful!
i have one doubt. what if the target is in non-increasing subarray
how can you find the strictly increasing by just comparing the mid-value with the left and right elements? At 10:35 you said mid element 2 is smaller than left value 7 so it is not strictly increasing but the 2 is also smaller than 6, the rightmost value, so how come second case id strictly increasing... so how can you judge which one strictly increasing and which one's not
absolutely brilliant..
Thanks :)
amazing explanation bro 👌🏽
thanks for the video. Helped me out a lot.
Nice :)
amazing explanation. Subscribed and liked :)
Thanks
What if the target element is present in the uneven part of the array? How does this code work then?
same doubt
See, if its in the part in which array is not sorted we will still apply binary search on it and check again whether its on sorted part or not, and do it till we find it
class Solution {
public:
int bsearch(vector&nums,int t,int l,int r){
while(lt)
r=m-1;
else
l=m+1;
}
return -1;
}
int lsearch(vector&nums,int t,int l,int r){
int m=(l+r)/2;
if(nums[l]
Thanks for the great tutorial amazing, can you add some modification how to handle duplicate values because its not working for duplicate values.
Good explanation
Awsome keep explaining this way..
Thanks :)
Hello Sir which app you use for writing purpose.
Brilliant Explaination!
Thanks :)
You deserve subscription.
Thanks :)
Great help bro. Thanks from punjab !!
Thanks paji
superb solution thank you sir :)
Welcome :)
amazing explanation
beautifully explained
Thanks :)
Great Explaination!!
Thanks :)
Can't we just check whether the target number is lies between left index and mid index. If not then search in right part?
way better than leetcode thanks
Can you post videos on Google Kickstart round A and B Solution
I think these are good question to practice and your explanation is quite better than other
Okay I will....but I am stuck with leetcode for this month bro 😅
@@techdose4u But you can post one video at a day similar to leetcode
I am stuck at these problem
I think your video will clear all my doubts having. Good explaination
Please send me the question link in which you are stuck. I will see.
great explanation sir. Thank you
Welcome :)
I believe,Searching through the peak element is a wrong solution even after ignoring need to traverse twice. For eg- 5 4 1 2 3 has peak elements 4 and 3. 3 is peak as 2
Indians are GREATTT at programming!!
such an amazing experience
Thanks :)
sir what if first we find pivot index around which array is sorted and then after comparison we can use binary search with appropriate range .this method will do or not
I explained this in Method 1. Please watch the video carefully. I have also provided the code for the same. So, it will definitely work na.
too awesome :)
Great Explanation!!
Thanks :)
What about for arrays with duplicates??
graph helps a lot in this question !!!!!!!!:)
👍
Thanks!
great video! other explanations on leetcode and youtube were much more complicated or had poor explanations.
Thanks
the literal excellent explanation.
Thanks
Excellent!!
Thanks
Simply brilliant
Thanks 😊
Nice one. Subscribed.
Thanks :)
great!
yes sir ,you deserve subscription + like + bell
Thanks ❤️
Thanks :)
Nice explaination :)
Thanks
Thanks
very nice explanation sir!!!!!!!!
Thanks
Got it. ThankYou
Liked + Subscribe + shared
Thanks
Thank u!!
Welcome :)
very nice!
Thanks :)
here your are using L+R/2 inorder to find mid. my doubt is if L and R both are int values and when you perform L+R then where this result will be stored because let say L and R are containing very big values (L=Integer.Max -2 and R=Integer.Max) then L+R well be overflaw , L+R will out of the range of integer and hence we will get wrong results. so, will L+R will be stored in some integer box(of memory) or somewhere else which can hold such a big value???
(2+3)/2 = 2 in integer. It is always the floor value.
@@techdose4u but suppose (2+3) is very very big number then it will go out of the range of integer then what will happen?
Integer wrap around INTMIN to INTMAX. So numbers too will wrap around simply
@@techdose4u yes, exactly but as it will start from INTMIN and that is from -ve range so, if it will give you -ve result Or Even if it will give positive result then also either you will get index out of bound error or you will get wrong mid index, isn't it???
You can add 1 GB matrix with another 1GB matrix and still you won't reach INT_MAX. That's how big INT_MAX is. So, don't worry. If you are adding range of matrix then it won't ever wrap around.
what happens if the case is [5,0,1,2,3,4]
What if the target is lying in the non uniform part? I have this doubt .... plz someone help
Very helpful
Thanks
Definitely first
:D
its not working for this input for me :
nums[]={1,3,1,1,1};
target= 3;
why are checking for = ????
what if we want to search an element , which is present in un-informed range
Element can be anything but array should be sorted rotated.
Hi do you do personaltraining if yes can i know how to contact. I am looking for full time opportunities now
Yes I do. Please contact on LinkedIn, Instagram, Whatsapp: +91 8918633037
how to do this
input---->[apple,ball,cat,dog] ,2,dog and
output--->1 (cat is at 2 and dog is at 3 so 3-2=1)
now
input----[aaa,bbb,ccc,ddd,eee] ,1,eee
output--->2 (bbb is at 1 and eee is at 4th index)
how to implement this