Search in rotated sorted array | Leetcode #33

Sdílet
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/...

Komentáře • 207

  • @arunraju9705
    @arunraju9705 Před 4 lety +110

    Dude, I appreciate the detailed presentation, you should know that you are doing a great service !

  • @klausdupont6335
    @klausdupont6335 Před 3 lety +17

    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 Před 4 lety +35

    last moment rap was awesome :D

  • @HanifCarroll
    @HanifCarroll Před 2 lety +7

    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.

  • @babulbhanu8935
    @babulbhanu8935 Před 2 lety +2

    got an offer from Microsoft after learning from this channel. Thanks :)

  • @chloe3337
    @chloe3337 Před 2 lety

    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!

  • @vikramreddy7586
    @vikramreddy7586 Před 4 lety +1

    From the 7th Minute onwards, this video is GOLD !!!!! Jaha Pana Tussi Great Ho !!!!!!

  • @davidpham6330
    @davidpham6330 Před rokem

    BEST explanation I've seen on CZcams for this problem. Thank you!

  • @omerbayram9533
    @omerbayram9533 Před 10 měsíci

    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!!!!

  • @xacademia9646
    @xacademia9646 Před rokem +1

    thanks I was gettin TLE in the first approach which I had thought by myself. Ur one pass solution really helped me :)

  • @abhishekmore5856
    @abhishekmore5856 Před 3 lety

    This is best explanation from all other explanations I have seen/read for this problem.

  • @ayushjain6604
    @ayushjain6604 Před 3 lety +4

    A really good explanation. Could not imagine that this problem could be solved in this way as well! Appreciate the work.

  • @jinny5025
    @jinny5025 Před 3 lety +6

    For dummies, your explaintion is always clear than ever. You deserve more subcribers. Thank you Tech Dose :)

  • @mj-sv7ep
    @mj-sv7ep Před 3 lety

    Very clear and precise explaination. Got it in the first go. Thanks

  • @saurabhchauhan232
    @saurabhchauhan232 Před 3 lety

    Excellent Explanation , haven't found better explanation of any problems than yours 🙏🏻

  • @denniskang6768
    @denniskang6768 Před 2 lety +2

    Looked at several other videos but still not clear. this one explains with great overall picture and logic ! thanks a lot !

  • @Chandrakumar-ub1uh
    @Chandrakumar-ub1uh Před 4 lety +2

    one of the best application of binary search, you explained it very well, the approach without finding pivot element is best approach

    • @techdose4u
      @techdose4u  Před 4 lety

      Thanks :) Actually using this approach, many harder problems are also solved.

  • @vamsiKrishna96
    @vamsiKrishna96 Před 3 lety +1

    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?

  • @tbcfrankee
    @tbcfrankee Před rokem

    Straightforward and clear! Thanks!

  • @samridhshubham8109
    @samridhshubham8109 Před 2 lety

    Very detailed and clear explanation, very thanks for all your effort

  • @priyankapardesiramachander871

    Sir, your channel deserves so much more attention and subs. Thank you.

  • @DavidCastro-sn3iy
    @DavidCastro-sn3iy Před 4 lety +4

    Great explanation! subscribed to the channel!... Keep up the good work

  • @Jason-ze8jv
    @Jason-ze8jv Před 3 lety

    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?

  • @ayushprakash8343
    @ayushprakash8343 Před 4 lety

    great ! i loved your explanation ... keep up the good work !

  • @roushansingh9936
    @roushansingh9936 Před rokem

    One of the finest explaination, Thank You!

  • @ranjith880
    @ranjith880 Před 2 lety +1

    Great visual presentation loved it

  • @nishadhshah3267
    @nishadhshah3267 Před 2 lety

    Finally understood this problem!!
    Thanks much!!

  • @aditgulia
    @aditgulia Před 4 lety

    At 12:06 , line 23 can also we written without checking target equal to mid elements. .ie
    line 23 : if(target

  • @akhiltyagi460
    @akhiltyagi460 Před 3 lety

    really appreciate your explanation skills.

  • @anshikagovil3774
    @anshikagovil3774 Před 4 lety +8

    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.

  • @haosmark
    @haosmark Před 11 měsíci

    Best explanation to the problem. Thanks for taking the time to make the video.

  • @shaswatpandey2991
    @shaswatpandey2991 Před 2 lety +1

    just amazing ! A huge appreciation for you.

  • @taocheng247
    @taocheng247 Před 2 lety +2

    This explantation is greater than any others.

  • @saubhagyadeep5488
    @saubhagyadeep5488 Před rokem

    gud work bro... the explanation was simple and examples were ample to be able to understand this easily :)

  • @samrat4141
    @samrat4141 Před 6 měsíci

    Thanks for the explanation. It really helps.

  • @luxfortis4377
    @luxfortis4377 Před rokem

    amazing explanation, thank you!

  • @nitinshukla4960
    @nitinshukla4960 Před 2 lety

    Really thanks buddy, Doing a great job :)

  • @omphemetsemafoko830
    @omphemetsemafoko830 Před 2 lety

    Best explanation ever. Thanks a lot.

  • @ayushisaini5311
    @ayushisaini5311 Před 2 lety +1

    Best content i have ever seen in youtube thankyou so much .we just need to know more nd more from you😊😊😊 ...

  • @lasophistique3513
    @lasophistique3513 Před 3 lety +1

    Very clear explanation, thank you!

  • @0anant0
    @0anant0 Před 3 lety +2

    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]

  • @sexyeur
    @sexyeur Před 4 lety

    Awesome! Thank you!

  • @shivaakrish
    @shivaakrish Před rokem

    Amazing explanation!

  • @priyadarshanr9950
    @priyadarshanr9950 Před 3 lety

    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 ?

  • @mayankkumarprajapati2337
    @mayankkumarprajapati2337 Před 3 lety +1

    Thankyou for such a great explanation really great video!!!!!

  • @anime__ash
    @anime__ash Před 2 lety

    Very Helpful!

  • @sujandvss2612
    @sujandvss2612 Před 3 lety +2

    i have one doubt. what if the target is in non-increasing subarray

  • @NIKHITAGARGBCE
    @NIKHITAGARGBCE Před 3 lety

    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

  • @sarthakbhatia7888
    @sarthakbhatia7888 Před 3 lety +7

    absolutely brilliant..

  • @kashifbilalali7609
    @kashifbilalali7609 Před 2 lety

    amazing explanation bro 👌🏽

  • @WowPlusWow
    @WowPlusWow Před 3 lety +1

    thanks for the video. Helped me out a lot.

  • @sushantmahajan5289
    @sushantmahajan5289 Před 3 lety +1

    amazing explanation. Subscribed and liked :)

  • @akshaykumarmalathkar2968
    @akshaykumarmalathkar2968 Před 3 lety +10

    What if the target element is present in the uneven part of the array? How does this code work then?

    • @nishanmurarka3137
      @nishanmurarka3137 Před 2 lety

      same doubt

    • @gyanendrasingh2003
      @gyanendrasingh2003 Před 2 lety +3

      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 Před 2 lety +1

      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]

  • @projectsdb4034
    @projectsdb4034 Před rokem

    Thanks for the great tutorial amazing, can you add some modification how to handle duplicate values because its not working for duplicate values.

  • @shashanksharma7747
    @shashanksharma7747 Před 2 lety

    Good explanation

  • @gayatri263
    @gayatri263 Před 3 lety +1

    Awsome keep explaining this way..

  • @shashankkumar1974
    @shashankkumar1974 Před 3 lety

    Hello Sir which app you use for writing purpose.

  • @shobhitranjan3957
    @shobhitranjan3957 Před 4 lety +3

    Brilliant Explaination!

  • @evgeni-nabokov
    @evgeni-nabokov Před 4 lety +3

    You deserve subscription.

  • @sehejwahla5437
    @sehejwahla5437 Před 3 lety +1

    Great help bro. Thanks from punjab !!

  • @ramnathan4236
    @ramnathan4236 Před 4 lety +3

    superb solution thank you sir :)

  • @rishabhkumar8115
    @rishabhkumar8115 Před 3 lety +1

    amazing explanation
    beautifully explained

  • @divanshuagarwal8236
    @divanshuagarwal8236 Před 4 lety +2

    Great Explaination!!

  • @VishalPatel_imvishal
    @VishalPatel_imvishal Před 3 lety

    Can't we just check whether the target number is lies between left index and mid index. If not then search in right part?

  • @4maxlol
    @4maxlol Před měsícem

    way better than leetcode thanks

  • @programmingstuff5741
    @programmingstuff5741 Před 4 lety +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  Před 4 lety +1

      Okay I will....but I am stuck with leetcode for this month bro 😅

    • @programmingstuff5741
      @programmingstuff5741 Před 4 lety +1

      @@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  Před 4 lety

      Please send me the question link in which you are stuck. I will see.

  • @shubhamsingh-gb5zh
    @shubhamsingh-gb5zh Před 3 lety +1

    great explanation sir. Thank you

  • @ayushganguli1359
    @ayushganguli1359 Před 4 lety +1

    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

  • @DucNguyen-bm2pl
    @DucNguyen-bm2pl Před 2 lety

    Indians are GREATTT at programming!!

  • @pankajrathi
    @pankajrathi Před 3 lety +1

    such an amazing experience

  • @vishalmishra1937
    @vishalmishra1937 Před 4 lety +1

    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  Před 4 lety

      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.

  • @fullysimplified7139
    @fullysimplified7139 Před 2 lety

    too awesome :)

  • @sushilmall254
    @sushilmall254 Před 4 lety

    Great Explanation!!

  • @khanmashequr
    @khanmashequr Před 3 lety

    What about for arrays with duplicates??

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt Před 3 lety +1

    graph helps a lot in this question !!!!!!!!:)

  • @seanmcelroy4074
    @seanmcelroy4074 Před 2 lety

    Thanks!

  • @apeirl5940
    @apeirl5940 Před 3 lety +1

    great video! other explanations on leetcode and youtube were much more complicated or had poor explanations.

  • @yy-gf7ze
    @yy-gf7ze Před 3 lety +1

    the literal excellent explanation.

  • @vipingautam1257
    @vipingautam1257 Před 3 lety +1

    Excellent!!

  • @princeanthony8525
    @princeanthony8525 Před 2 lety +1

    Simply brilliant

  • @TeddyDeanShorts
    @TeddyDeanShorts Před 3 lety +1

    Nice one. Subscribed.

  • @immortal7167
    @immortal7167 Před 2 lety

    great!

  • @pathanshanawajkhan2638
    @pathanshanawajkhan2638 Před 3 lety +2

    yes sir ,you deserve subscription + like + bell

  • @sharvyahmed
    @sharvyahmed Před 2 lety

    Thanks :)

  • @rajatgupta-ld1wg
    @rajatgupta-ld1wg Před 3 lety +1

    Nice explaination :)

  • @fullysimplified7139
    @fullysimplified7139 Před 2 lety

    Thanks

  • @rohandeshmukh3989
    @rohandeshmukh3989 Před 3 lety +1

    very nice explanation sir!!!!!!!!

  • @gokullohiya2602
    @gokullohiya2602 Před 2 lety +1

    Got it. ThankYou

  • @meghanaballa4966
    @meghanaballa4966 Před 2 lety +1

    Thank u!!

  • @gauravruhela007
    @gauravruhela007 Před 3 lety +1

    very nice!

  • @yashgoswami5374
    @yashgoswami5374 Před 4 lety +1

    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  Před 4 lety

      (2+3)/2 = 2 in integer. It is always the floor value.

    • @yashgoswami5374
      @yashgoswami5374 Před 4 lety +1

      @@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  Před 4 lety

      Integer wrap around INTMIN to INTMAX. So numbers too will wrap around simply

    • @yashgoswami5374
      @yashgoswami5374 Před 4 lety

      @@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  Před 4 lety +1

      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.

  • @shaswatsingh
    @shaswatsingh Před 3 lety

    what happens if the case is [5,0,1,2,3,4]

  • @ayushthakur2896
    @ayushthakur2896 Před 3 lety

    What if the target is lying in the non uniform part? I have this doubt .... plz someone help

  • @priyanshjha6952
    @priyanshjha6952 Před 3 lety +1

    Very helpful

  • @mohammedsadiq5729
    @mohammedsadiq5729 Před 4 lety +3

    Definitely first

  • @asitkr7
    @asitkr7 Před 3 lety

    its not working for this input for me :
    nums[]={1,3,1,1,1};
    target= 3;

  • @apuravsharma7034
    @apuravsharma7034 Před 3 lety

    why are checking for = ????

  • @sumanthreddynithiin2109
    @sumanthreddynithiin2109 Před 3 lety +1

    what if we want to search an element , which is present in un-informed range

    • @techdose4u
      @techdose4u  Před 3 lety

      Element can be anything but array should be sorted rotated.

  • @prashumatam9816
    @prashumatam9816 Před 2 lety +1

    Hi do you do personaltraining if yes can i know how to contact. I am looking for full time opportunities now

    • @techdose4u
      @techdose4u  Před 2 lety

      Yes I do. Please contact on LinkedIn, Instagram, Whatsapp: +91 8918633037

  • @vikrantharne9345
    @vikrantharne9345 Před 3 lety

    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