No video

Search in rotated sorted array | Leetcode #33

  Рет қаралды 83,118

Techdose

Techdose

4 жыл бұрын

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/...

Пікірлер: 207
@arunraju9705
@arunraju9705 4 жыл бұрын
Dude, I appreciate the detailed presentation, you should know that you are doing a great service !
@techdose4u
@techdose4u 4 жыл бұрын
Thanks:)
@justinspired4188
@justinspired4188 3 жыл бұрын
Totally agreed bro!! he is doing really good job, thanks !!
@klausdupont6335
@klausdupont6335 3 жыл бұрын
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!
@amansarma417
@amansarma417 4 жыл бұрын
last moment rap was awesome :D
@SHAHARPIT92
@SHAHARPIT92 4 жыл бұрын
Even I liked it :P
@techdose4u
@techdose4u 4 жыл бұрын
😅
@HanifCarroll
@HanifCarroll 2 жыл бұрын
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.
@chloe3337
@chloe3337 2 жыл бұрын
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!
@babulbhanu8935
@babulbhanu8935 2 жыл бұрын
got an offer from Microsoft after learning from this channel. Thanks :)
@jinny5025
@jinny5025 3 жыл бұрын
For dummies, your explaintion is always clear than ever. You deserve more subcribers. Thank you Tech Dose :)
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@ayushjain6604
@ayushjain6604 3 жыл бұрын
A really good explanation. Could not imagine that this problem could be solved in this way as well! Appreciate the work.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@xacademia9646
@xacademia9646 Жыл бұрын
thanks I was gettin TLE in the first approach which I had thought by myself. Ur one pass solution really helped me :)
@vikramreddy7586
@vikramreddy7586 4 жыл бұрын
From the 7th Minute onwards, this video is GOLD !!!!! Jaha Pana Tussi Great Ho !!!!!!
@Ice-2706
@Ice-2706 4 жыл бұрын
great ! i loved your explanation ... keep up the good work !
@Chandrakumar-ub1uh
@Chandrakumar-ub1uh 4 жыл бұрын
one of the best application of binary search, you explained it very well, the approach without finding pivot element is best approach
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :) Actually using this approach, many harder problems are also solved.
@DavidCastro-sn3iy
@DavidCastro-sn3iy 4 жыл бұрын
Great explanation! subscribed to the channel!... Keep up the good work
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@mj-sv7ep
@mj-sv7ep 3 жыл бұрын
Very clear and precise explaination. Got it in the first go. Thanks
@abhishekmore5856
@abhishekmore5856 3 жыл бұрын
This is best explanation from all other explanations I have seen/read for this problem.
@tbcfrankee
@tbcfrankee Жыл бұрын
Straightforward and clear! Thanks!
@davidpham6330
@davidpham6330 Жыл бұрын
BEST explanation I've seen on KZbin for this problem. Thank you!
@denniskang6768
@denniskang6768 2 жыл бұрын
Looked at several other videos but still not clear. this one explains with great overall picture and logic ! thanks a lot !
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😀
@saurabhchauhan232
@saurabhchauhan232 3 жыл бұрын
Excellent Explanation , haven't found better explanation of any problems than yours 🙏🏻
@sexyeur
@sexyeur 4 жыл бұрын
Awesome! Thank you!
@UCA12jryuRctKIGCxDkbub0Q
@UCA12jryuRctKIGCxDkbub0Q 11 ай бұрын
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!!!!
@akhiltyagi460
@akhiltyagi460 4 жыл бұрын
really appreciate your explanation skills.
@ranjith880
@ranjith880 3 жыл бұрын
Great visual presentation loved it
@samridhshubham8109
@samridhshubham8109 2 жыл бұрын
Very detailed and clear explanation, very thanks for all your effort
@roushansingh9936
@roushansingh9936 Жыл бұрын
One of the finest explaination, Thank You!
@luxfortis4377
@luxfortis4377 Жыл бұрын
amazing explanation, thank you!
@priyankapardesiramachander871
@priyankapardesiramachander871 3 жыл бұрын
Sir, your channel deserves so much more attention and subs. Thank you.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks ❤️
@Jason-ze8jv
@Jason-ze8jv 3 жыл бұрын
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?
@nishadhshah3267
@nishadhshah3267 2 жыл бұрын
Finally understood this problem!! Thanks much!!
@lasophistique3513
@lasophistique3513 4 жыл бұрын
Very clear explanation, thank you!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@anime__ash
@anime__ash 2 жыл бұрын
Very Helpful!
@aditgulia
@aditgulia 4 жыл бұрын
At 12:06 , line 23 can also we written without checking target equal to mid elements. .ie line 23 : if(target
@shivaakrish
@shivaakrish Жыл бұрын
Amazing explanation!
@anshikagovil3774
@anshikagovil3774 4 жыл бұрын
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.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@sarthakbhatia7888
@sarthakbhatia7888 3 жыл бұрын
absolutely brilliant..
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@samrat4141
@samrat4141 7 ай бұрын
Thanks for the explanation. It really helps.
@shaswatpandey2991
@shaswatpandey2991 2 жыл бұрын
just amazing ! A huge appreciation for you.
@techdose4u
@techdose4u 2 жыл бұрын
Thanks :)
@mayankkumarprajapati2337
@mayankkumarprajapati2337 3 жыл бұрын
Thankyou for such a great explanation really great video!!!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@haosmark
@haosmark Жыл бұрын
Best explanation to the problem. Thanks for taking the time to make the video.
@WowPlusWow
@WowPlusWow 4 жыл бұрын
thanks for the video. Helped me out a lot.
@techdose4u
@techdose4u 4 жыл бұрын
Nice :)
@vamsiKrishna96
@vamsiKrishna96 3 жыл бұрын
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?
@shobhitranjan3957
@shobhitranjan3957 4 жыл бұрын
Brilliant Explaination!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@nitinshukla4960
@nitinshukla4960 2 жыл бұрын
Really thanks buddy, Doing a great job :)
@taocheng247
@taocheng247 2 жыл бұрын
This explantation is greater than any others.
@techdose4u
@techdose4u 2 жыл бұрын
❤️ Thanks
@omphemetsemafoko830
@omphemetsemafoko830 2 жыл бұрын
Best explanation ever. Thanks a lot.
@divanshuagarwal8236
@divanshuagarwal8236 4 жыл бұрын
Great Explaination!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@kashifbilalali7609
@kashifbilalali7609 2 жыл бұрын
amazing explanation bro 👌🏽
@pankajrathi
@pankajrathi 3 жыл бұрын
such an amazing experience
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@ayushisaini5311
@ayushisaini5311 2 жыл бұрын
Best content i have ever seen in youtube thankyou so much .we just need to know more nd more from you😊😊😊 ...
@techdose4u
@techdose4u 2 жыл бұрын
Thanks ❤️
@sushantmahajan5289
@sushantmahajan5289 3 жыл бұрын
amazing explanation. Subscribed and liked :)
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@sushilmall254
@sushilmall254 4 жыл бұрын
Great Explanation!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@ramnathan4236
@ramnathan4236 4 жыл бұрын
superb solution thank you sir :)
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@saubhagyadeep5488
@saubhagyadeep5488 Жыл бұрын
gud work bro... the explanation was simple and examples were ample to be able to understand this easily :)
@gayatri263
@gayatri263 3 жыл бұрын
Awsome keep explaining this way..
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@vipingautam1257
@vipingautam1257 3 жыл бұрын
Excellent!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@evgeni-nabokov
@evgeni-nabokov 4 жыл бұрын
You deserve subscription.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@projectsdb4034
@projectsdb4034 Жыл бұрын
Thanks for the great tutorial amazing, can you add some modification how to handle duplicate values because its not working for duplicate values.
@sehejwahla5437
@sehejwahla5437 3 жыл бұрын
Great help bro. Thanks from punjab !!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks paji
@TeddyDeanShorts
@TeddyDeanShorts 3 жыл бұрын
Nice one. Subscribed.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@sujandvss2612
@sujandvss2612 3 жыл бұрын
i have one doubt. what if the target is in non-increasing subarray
@shashanksharma7747
@shashanksharma7747 2 жыл бұрын
Good explanation
@rishabhkumar8115
@rishabhkumar8115 3 жыл бұрын
amazing explanation beautifully explained
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@seanmcelroy4074
@seanmcelroy4074 2 жыл бұрын
Thanks!
@yy-gf7ze
@yy-gf7ze 3 жыл бұрын
the literal excellent explanation.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@princeanthony8525
@princeanthony8525 2 жыл бұрын
Simply brilliant
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@shubhamsingh-gb5zh
@shubhamsingh-gb5zh 3 жыл бұрын
great explanation sir. Thank you
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@fullysimplified7139
@fullysimplified7139 3 жыл бұрын
too awesome :)
@0anant0
@0anant0 3 жыл бұрын
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]
@pranavsharma7479
@pranavsharma7479 2 жыл бұрын
same ques
@4maxlol
@4maxlol 2 ай бұрын
way better than leetcode thanks
@rajatgupta-ld1wg
@rajatgupta-ld1wg 3 жыл бұрын
Nice explaination :)
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@meghanaballa4966
@meghanaballa4966 2 жыл бұрын
Thank u!!
@techdose4u
@techdose4u 2 жыл бұрын
Welcome :)
@programmingstuff5741
@programmingstuff5741 4 жыл бұрын
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
@techdose4u
@techdose4u 4 жыл бұрын
Okay I will....but I am stuck with leetcode for this month bro 😅
@programmingstuff5741
@programmingstuff5741 4 жыл бұрын
@@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
@techdose4u
@techdose4u 4 жыл бұрын
Please send me the question link in which you are stuck. I will see.
@mahipalsingh-yo4jt
@mahipalsingh-yo4jt 3 жыл бұрын
graph helps a lot in this question !!!!!!!!:)
@techdose4u
@techdose4u 3 жыл бұрын
👍
@akshaykumarmalathkar2968
@akshaykumarmalathkar2968 3 жыл бұрын
What if the target element is present in the uneven part of the array? How does this code work then?
@nishanmurarka3137
@nishanmurarka3137 2 жыл бұрын
same doubt
@gyanendrasingh2003
@gyanendrasingh2003 2 жыл бұрын
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
@shivanshgupta5657
@shivanshgupta5657 2 жыл бұрын
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]
@sharvyahmed
@sharvyahmed 2 жыл бұрын
Thanks :)
@gokullohiya2602
@gokullohiya2602 3 жыл бұрын
Got it. ThankYou
@gokullohiya2602
@gokullohiya2602 3 жыл бұрын
Liked + Subscribe + shared
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@rohandeshmukh3989
@rohandeshmukh3989 3 жыл бұрын
very nice explanation sir!!!!!!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@NIKHITAGARGBCE
@NIKHITAGARGBCE 3 жыл бұрын
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
@apeirl5940
@apeirl5940 3 жыл бұрын
great video! other explanations on leetcode and youtube were much more complicated or had poor explanations.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@priyadarshanr9950
@priyadarshanr9950 3 жыл бұрын
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 ?
@immortal7167
@immortal7167 2 жыл бұрын
great!
@ayushganguli1359
@ayushganguli1359 4 жыл бұрын
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
@fullysimplified7139
@fullysimplified7139 3 жыл бұрын
Thanks
@VishalPatel_imvishal
@VishalPatel_imvishal 3 жыл бұрын
Can't we just check whether the target number is lies between left index and mid index. If not then search in right part?
@pathanshanawajkhan2638
@pathanshanawajkhan2638 3 жыл бұрын
yes sir ,you deserve subscription + like + bell
@techdose4u
@techdose4u 3 жыл бұрын
Thanks ❤️
@gauravruhela007
@gauravruhela007 3 жыл бұрын
very nice!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@vishalmishra1937
@vishalmishra1937 4 жыл бұрын
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
@techdose4u
@techdose4u 4 жыл бұрын
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.
@priyanshjha6952
@priyanshjha6952 3 жыл бұрын
Very helpful
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@shashankkumar1974
@shashankkumar1974 3 жыл бұрын
Hello Sir which app you use for writing purpose.
@khanmashequr
@khanmashequr 4 жыл бұрын
What about for arrays with duplicates??
@shaswatsingh
@shaswatsingh 3 жыл бұрын
what happens if the case is [5,0,1,2,3,4]
@ayushthakur2896
@ayushthakur2896 3 жыл бұрын
What if the target is lying in the non uniform part? I have this doubt .... plz someone help
@DucNguyen-bm2pl
@DucNguyen-bm2pl 2 жыл бұрын
Indians are GREATTT at programming!!
@asitkr7
@asitkr7 3 жыл бұрын
its not working for this input for me : nums[]={1,3,1,1,1}; target= 3;
@yashgoswami5374
@yashgoswami5374 4 жыл бұрын
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???
@techdose4u
@techdose4u 4 жыл бұрын
(2+3)/2 = 2 in integer. It is always the floor value.
@yashgoswami5374
@yashgoswami5374 4 жыл бұрын
@@techdose4u but suppose (2+3) is very very big number then it will go out of the range of integer then what will happen?
@techdose4u
@techdose4u 4 жыл бұрын
Integer wrap around INTMIN to INTMAX. So numbers too will wrap around simply
@yashgoswami5374
@yashgoswami5374 4 жыл бұрын
@@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???
@techdose4u
@techdose4u 4 жыл бұрын
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.
@sumanthreddynithiin2109
@sumanthreddynithiin2109 4 жыл бұрын
what if we want to search an element , which is present in un-informed range
@techdose4u
@techdose4u 4 жыл бұрын
Element can be anything but array should be sorted rotated.
@apuravsharma7034
@apuravsharma7034 3 жыл бұрын
why are checking for = ????
@vikrantharne9345
@vikrantharne9345 3 жыл бұрын
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
@keerthi442
@keerthi442 4 жыл бұрын
Bro,I have campus interview after 2 month Can you plz suggest what to study I'm flowing all your videos I like your videos a lot
@techdose4u
@techdose4u 4 жыл бұрын
I have made a video on preperation in just 2 months. If you different help or you are already good in certain fields then contact me on LinkedIn.
@anubhavraj2072
@anubhavraj2072 4 жыл бұрын
Don't worry ,it will be postponed because of the pandemic. You can go through GFG
@hrithiksytle5058
@hrithiksytle5058 4 жыл бұрын
@@techdose4u can you give your LinkedIn ID And can you mention in what Company do you work ??
@shubhamsonal5871
@shubhamsonal5871 4 жыл бұрын
What if the element was present on the non increasing sub half , then we would never visit that half, so maybe this wont work for that case
@techdose4u
@techdose4u 4 жыл бұрын
I said you need to find the increasing partition. Then compare the target with the range of increasing half. If target falls in the range then search in that increasing half otherwise search in the other half.
@f3-faithfitnessfinance
@f3-faithfitnessfinance 4 жыл бұрын
This will work for all cases ! It checks the increasing sub half as well . That's in the 'else' condition
@techdose4u
@techdose4u 4 жыл бұрын
Yes you are correct :)
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 225 М.
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 318 М.
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 12 МЛН
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 10 МЛН
Product of array except self | Leetcode #238
15:00
Techdose
Рет қаралды 94 М.
Search in Rotated Sorted Array II - Leetcode 81 - Python
17:36
NeetCodeIO
Рет қаралды 13 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 292 М.
Largest rectangle in Histogram | Leetcode #84
27:43
Techdose
Рет қаралды 109 М.
The 3 Levels of Binary Search
22:06
Byte by Byte
Рет қаралды 15 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 148 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 863 М.
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 12 МЛН