Search in rotated sorted array | Leetcode #33

Поділитися
Вставка
  • Опубліковано 9 лис 2024

КОМЕНТАРІ • 209

  • @arunraju9705
    @arunraju9705 4 роки тому +113

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

  • @klausdupont6335
    @klausdupont6335 3 роки тому +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!

  • @HanifCarroll
    @HanifCarroll 3 роки тому +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 3 роки тому +2

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

  • @jinny5025
    @jinny5025 3 роки тому +6

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

  • @xacademia9646
    @xacademia9646 2 роки тому +1

    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 роки тому +2

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

  • @denniskang6768
    @denniskang6768 2 роки тому +2

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

  • @UCA12jryuRctKIGCxDkbub0Q
    @UCA12jryuRctKIGCxDkbub0Q Рік тому

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

  • @chloe3337
    @chloe3337 3 роки тому

    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!

  • @Chandrakumar-ub1uh
    @Chandrakumar-ub1uh 4 роки тому +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  4 роки тому

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

  • @aditgulia
    @aditgulia 4 роки тому

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

  • @davidpham6330
    @davidpham6330 Рік тому

    BEST explanation I've seen on UA-cam for this problem. Thank you!

  • @priyankapardesiramachander871
    @priyankapardesiramachander871 3 роки тому +2

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

  • @amansarma417
    @amansarma417 4 роки тому +34

    last moment rap was awesome :D

  • @anshikagovil3774
    @anshikagovil3774 4 роки тому +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.

  • @ayushjain6604
    @ayushjain6604 4 роки тому +4

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

  • @abhishekmore5856
    @abhishekmore5856 3 роки тому

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

  • @taocheng247
    @taocheng247 2 роки тому +2

    This explantation is greater than any others.

  • @DucNguyen-bm2pl
    @DucNguyen-bm2pl 2 роки тому

    Indians are GREATTT at programming!!

  • @programmingstuff5741
    @programmingstuff5741 4 роки тому +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 роки тому +1

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

    • @programmingstuff5741
      @programmingstuff5741 4 роки тому +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  4 роки тому

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

  • @haosmark
    @haosmark Рік тому

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

  • @roushansingh9936
    @roushansingh9936 Рік тому

    One of the finest explaination, Thank You!

  • @ayushisaini5311
    @ayushisaini5311 2 роки тому +1

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

  • @0anant0
    @0anant0 4 роки тому +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]

  • @ranjith880
    @ranjith880 3 роки тому +1

    Great visual presentation loved it

  • @saurabhchauhan232
    @saurabhchauhan232 3 роки тому

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

  • @samridhshubham8109
    @samridhshubham8109 2 роки тому

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

  • @palashmanandhar75
    @palashmanandhar75 2 місяці тому

    finally understood the visualization. thank you

  • @shaswatpandey2991
    @shaswatpandey2991 2 роки тому +1

    just amazing ! A huge appreciation for you.

  • @nishadhshah3267
    @nishadhshah3267 2 роки тому

    Finally understood this problem!!
    Thanks much!!

  • @mj-sv7ep
    @mj-sv7ep 3 роки тому

    Very clear and precise explaination. Got it in the first go. 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.

  • @rishabhkumar8115
    @rishabhkumar8115 3 роки тому +1

    amazing explanation
    beautifully explained

  • @samrat4141
    @samrat4141 10 місяців тому

    Thanks for the explanation. It really helps.

  • @akshaykumarmalathkar2968
    @akshaykumarmalathkar2968 3 роки тому +10

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

    • @nishanmurarka3137
      @nishanmurarka3137 3 роки тому

      same doubt

    • @gyanendrasingh2003
      @gyanendrasingh2003 3 роки тому +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

    • @shivansh-gup
      @shivansh-gup 2 роки тому +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]

  • @DavidCastro-sn3iy
    @DavidCastro-sn3iy 4 роки тому +4

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

  • @omphemetsemafoko830
    @omphemetsemafoko830 2 роки тому

    Best explanation ever. Thanks a lot.

  • @sarthakbhatia7888
    @sarthakbhatia7888 4 роки тому +7

    absolutely brilliant..

  • @gayatri263
    @gayatri263 3 роки тому +1

    Awsome keep explaining this way..

  • @evgeni-nabokov
    @evgeni-nabokov 4 роки тому +3

    You deserve subscription.

  • @tbcfrankee
    @tbcfrankee 2 роки тому

    Straightforward and clear! Thanks!

  • @princeanthony8525
    @princeanthony8525 2 роки тому +1

    Simply brilliant

  • @saubhagyadeep5488
    @saubhagyadeep5488 2 роки тому

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

  • @4maxlol
    @4maxlol 5 місяців тому

    way better than leetcode thanks

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt 3 роки тому +1

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

  • @ayushganguli1359
    @ayushganguli1359 4 роки тому +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

  • @rohandeshmukh3989
    @rohandeshmukh3989 3 роки тому +1

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

  • @luxfortis4377
    @luxfortis4377 Рік тому

    amazing explanation, thank you!

  • @adhirajmajumder
    @adhirajmajumder 2 роки тому

    def find_in_sorted_array(nums,target):
    n=len(nums)
    l=0
    r=n-1
    first=nums[0]
    while(r>=l):
    mid=int((l+r)/2)
    value=nums[mid]
    if (value==target):
    return mid
    am_big=value>=first
    target_big=target>=first
    if(am_big==target_big):
    if(value

  • @akhiltyagi460
    @akhiltyagi460 4 роки тому

    really appreciate your explanation skills.

  • @shashanksharma7747
    @shashanksharma7747 2 роки тому

    Good explanation

  • @yy-gf7ze
    @yy-gf7ze 3 роки тому +1

    the literal excellent explanation.

  • @shobhitranjan3957
    @shobhitranjan3957 4 роки тому +3

    Brilliant Explaination!

  • @nationanime1074
    @nationanime1074 2 роки тому

    thanks for the great explanation... It was really helpful...

  • @kashifbilalali7609
    @kashifbilalali7609 2 роки тому

    amazing explanation bro 👌🏽

  • @shubhamsingh-gb5zh
    @shubhamsingh-gb5zh 3 роки тому +1

    great explanation sir. Thank you

  • @vamsiKrishna96
    @vamsiKrishna96 3 роки тому +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?

  • @apeirl5940
    @apeirl5940 3 роки тому +1

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

  • @ramnathan4236
    @ramnathan4236 4 роки тому +3

    superb solution thank you sir :)

  • @sehejwahla5437
    @sehejwahla5437 4 роки тому +1

    Great help bro. Thanks from punjab !!

  • @sushantmahajan5289
    @sushantmahajan5289 3 роки тому +1

    amazing explanation. Subscribed and liked :)

  • @pathanshanawajkhan2638
    @pathanshanawajkhan2638 4 роки тому +2

    yes sir ,you deserve subscription + like + bell

  • @TeddyDeanShorts
    @TeddyDeanShorts 3 роки тому +1

    Nice one. Subscribed.

  • @sujandvss2612
    @sujandvss2612 3 роки тому +2

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

  • @vipingautam1257
    @vipingautam1257 3 роки тому +1

    Excellent!!

  • @amazed4778
    @amazed4778 2 роки тому +1

    Amazing explanation!

  • @lasophistique3513
    @lasophistique3513 4 роки тому +1

    Very clear explanation, thank you!

  • @rajatgupta-ld1wg
    @rajatgupta-ld1wg 4 роки тому +1

    Nice explaination :)

  • @mayankkumarprajapati2337
    @mayankkumarprajapati2337 4 роки тому +1

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

  • @pha1994
    @pha1994 3 роки тому +1

    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    lo = 0
    hi = len(nums) - 1
    while lo = nums[lo]: # Get the monotonic increasing part
    if nums[mid] >= target >= nums[lo]: # If target is in the monotonic part
    hi = mid - 1
    else: # If target is on the other side of the monotonic part
    lo = mid + 1
    else: # OR Get the other monotonic increasing part
    if nums[mid]

  • @hrithiksytle5058
    @hrithiksytle5058 4 роки тому +2

    Python implementation
    def Solution (arr , target):
    try:
    return arr.index(target)
    except:
    return -1
    Python default List.index() method works for this in Log(n) time complexity.
    If element is not present due to exception raised we handle it by returning -1 as target element is not present in List.

    • @techdose4u
      @techdose4u  4 роки тому

      Nice

    • @mohituniyal3008
      @mohituniyal3008 4 роки тому

      Take a look at this discussion which says List.index method has O(N) time complexity : stackoverflow.com/questions/5913671/complexity-of-list-indexx-in-python

    • @maheshreddy4080
      @maheshreddy4080 4 роки тому

      It's not logn its o(n)

  • @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?

  • @yashgoswami5374
    @yashgoswami5374 4 роки тому +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  4 роки тому

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

    • @yashgoswami5374
      @yashgoswami5374 4 роки тому +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  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 роки тому +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.

  • @nitinshukla4960
    @nitinshukla4960 3 роки тому

    Really thanks buddy, Doing a great job :)

  • @divanshuagarwal8236
    @divanshuagarwal8236 4 роки тому +2

    Great Explaination!!

  • @anime__ash
    @anime__ash 2 роки тому

    Very Helpful!

  • @keerthi442
    @keerthi442 4 роки тому +3

    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 роки тому +1

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

  • @Ice-2706
    @Ice-2706 4 роки тому

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

  • @shashankkumar1974
    @shashankkumar1974 3 роки тому

    Hello Sir which app you use for writing purpose.

  • @priyanshjha6952
    @priyanshjha6952 4 роки тому +1

    Very helpful

  • @WowPlusWow
    @WowPlusWow 4 роки тому +1

    thanks for the video. Helped me out a lot.

  • @gokullohiya2602
    @gokullohiya2602 3 роки тому +1

    Got it. ThankYou

  • @pankajrathi
    @pankajrathi 4 роки тому +1

    such an amazing experience

  • @vishalmishra1937
    @vishalmishra1937 4 роки тому +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  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.

  • @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

  • @Yash-uk8ib
    @Yash-uk8ib 4 роки тому +2

    pivot element is always the maximum element of the array?

    • @Yash-uk8ib
      @Yash-uk8ib 4 роки тому

      in this case

    • @techdose4u
      @techdose4u  4 роки тому +1

      If the effective rotation is 0 then pivot doesn't matter. If we have effective rotation then I have assumed pivot element to be the larger element. You can assume smaller one and code as well. Both are correct.

  • @meghanaballa4966
    @meghanaballa4966 3 роки тому +1

    Thank u!!

  • @priyadarshanr296
    @priyadarshanr296 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 ?

  • @gauravruhela007
    @gauravruhela007 3 роки тому +1

    very nice!

  • @f3-faithfitnessfinance
    @f3-faithfitnessfinance 4 роки тому +3

    Definitely first

  • @sushilmall254
    @sushilmall254 4 роки тому

    Great Explanation!!

  • @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?

  • @habeebah6135
    @habeebah6135 25 днів тому

    Thank you so much 😭

  • @fullysimplified7139
    @fullysimplified7139 3 роки тому

    Thanks

  • @fullysimplified7139
    @fullysimplified7139 3 роки тому

    too awesome :)

  • @seanmcelroy4074
    @seanmcelroy4074 2 роки тому

    Thanks!

  • @ritavdas7570
    @ritavdas7570 3 роки тому +1

    That was a great explanation . But can anyone just explain me how was the array rotated 5 times ?

    • @abhisheksaraf2616
      @abhisheksaraf2616 3 роки тому

      Because minimum element which is 0 in this case is shifted right towards from its original position

  • @shubhamsonal5871
    @shubhamsonal5871 4 роки тому +2

    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 роки тому +1

      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 роки тому +1

      Yes you are correct :)

  • @prashumatam9816
    @prashumatam9816 3 роки тому +1

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

    • @techdose4u
      @techdose4u  3 роки тому

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