BS-5. Search Element in Rotated Sorted Array II

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • Problem Link: bit.ly/3MCdLTY
    Notes/C++/Java/Python codes: takeuforward.o...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/take...
    0:00 Introduction of Course

Komentáře • 350

  • @takeUforward
    @takeUforward  Před rokem +61

    Please comment understood and give us a like if you got everything :)

    • @FactFactoryCentral
      @FactFactoryCentral Před rokem +7

      In this code, isn't the worst case O(N). If all elements are same, and the element to be searched is not there in the array.
      Example: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3], target: 4
      Is O(N) the best that can be done if we have duplicate elements ?

    • @sudhanshushekhar4222
      @sudhanshushekhar4222 Před rokem

      understood

    • @venup2813
      @venup2813 Před rokem

      Understood

    • @abhisheksinghmehra9576
      @abhisheksinghmehra9576 Před rokem

      Understood

    • @365tage9
      @365tage9 Před 8 měsíci

      Understood. You are a genius.

  • @hardikpatel352
    @hardikpatel352 Před 4 měsíci +11

    there are lots of paid courses are available and many are upcoming but none of them is such in-depth and free like striver's A to Z dsa , Thanks a lot raj sir 🙇🏿‍♂🙇🏿‍♂

  • @SumitSingh-dc8pm
    @SumitSingh-dc8pm Před 9 měsíci +15

    You just are a legend. Hat's off to you man. Loved your content. You start from basics and made this concept a cakewalk. Huge Respect for you.

  • @darshilmodi6851
    @darshilmodi6851 Před 3 měsíci +3

    I truly appreciate your detailed explanation! To contribute to the community's knowledge, I encountered a situation where the original logic using low++ and high-- to handle duplicates wouldn't work.
    Here's a specific test case that demonstrates the issue: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1].
    Fortunately, a simple fix exists. Instead of decrementing both low and high, we can simply increment low like this: low++. This modification allows the code to function correctly.

    • @srujangajjala2658
      @srujangajjala2658 Před 3 měsíci

      ur modification isnt working for ur test case bro

    • @srujangajjala2658
      @srujangajjala2658 Před 3 měsíci

      there is some error in coding ninjas, i ran the same code in offline compiler with ur test case it returned true only, in coding ninjas its returning false

    • @t3ch_r4id
      @t3ch_r4id Před měsícem

      I have submitted the code in coding ninja successfully the problem is if you carefully observe the question it states that array will be right rotated not left rotated 💡

    • @aishezsingh7004
      @aishezsingh7004 Před měsícem

      @@t3ch_r4id His Testcase is achivable by left rotating at 5th index

    • @monishamadu
      @monishamadu Před 11 dny

      It worked for me when I call the recursive method by just incrementing lower point, instead of continue.

  • @depavathnareshnaik5584
    @depavathnareshnaik5584 Před 3 měsíci

    Thank you for the excellent instruction on BS. Your clear explanations and engaging teaching methods really helped me grasp the concepts thoroughly. I feel much more confident in my understanding now. I appreciate your dedication and support!

  • @Anshydv3
    @Anshydv3 Před rokem +2

    the best explaination , i have ever seen on binary Search , LOVE YOU BRO ❣

  • @LearnwithEase20
    @LearnwithEase20 Před 8 měsíci +2

    Becoming your bigger fan day by day! Hats to your explanation

  • @AppaniMadhavi
    @AppaniMadhavi Před 7 měsíci +1

    The code for the rotated sorted array I is also worked for rotated sorted array II in coding ninjas, but coding ninjas didn't provided all edge test cases.

    • @harshvardhansankpal716
      @harshvardhansankpal716 Před 4 měsíci

      yup in leetcode, that edge case of 2 elements is covere [ 3, 1 ] here high crosses low

  • @akbunofficial1281
    @akbunofficial1281 Před 10 měsíci +2

    Better way to avoid the edge case of arr[mid]=arr[low]=arr[high] is to check the right sorted first , this will make the code more efficient

    • @manusklm1161
      @manusklm1161 Před 15 dny

      Really? Can you explain with an example?

  • @-ShangsitNath
    @-ShangsitNath Před 4 měsíci +2

    Worst case time complexity will become O(n) ?? Eg. if array = [3,3,3,3,3,3,3,3], target = 1 ??

    • @kushwanthkapa2041
      @kushwanthkapa2041 Před 2 měsíci

      Yup😊

    • @manusklm1161
      @manusklm1161 Před 15 dny

      Nope...it's n/2

    • @EerieEntertainment-mc4ce
      @EerieEntertainment-mc4ce Před 14 dny

      @@manusklm1161 O(n/2) is also order of n

    • @manusklm1161
      @manusklm1161 Před 7 dny

      @@EerieEntertainment-mc4ce yeah I too know that we won't consider constants .but first we should able to find as precisely as possible, right and then you can do that stuff like neglecting constants etc.

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr Před rokem +2

    Understood the intuition and approach. Thanks for the series.

  • @mehulthuletiya497
    @mehulthuletiya497 Před rokem +16

    Time-Stamps:
    00:32 Problem Statement
    00:50 Recap: Search Element in Rotated Sorted Array I
    01:28 Why this problem different from previous
    05:38 Explanation & Approach Start (Search Element in Rotated Sorted Array II)
    10:09 Code
    10:19 Complexity
    11:41 Tip to solve problem

  • @cinime
    @cinime Před rokem +2

    Understood! Wonderful explanation as always, thank you very very much for your continuous effort!!

  • @venkatsaireddy1412
    @venkatsaireddy1412 Před rokem +3

    You are always the best bro, Thank you for clear explanation.

  • @prathambhushan4859
    @prathambhushan4859 Před 2 měsíci

    didn't studied anyhing since last week, but starting again and gonna complete this in this week

  • @VikashKumar-tg3ot
    @VikashKumar-tg3ot Před 4 měsíci

    Understood very well,Now i able to code my self before watching solution.Thanks striver bro.

  • @stith_pragya
    @stith_pragya Před 7 měsíci +1

    UNDERSTOOD.........Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @JK-de2gh
    @JK-de2gh Před 2 měsíci +1

    hatsoff to u mann...put some python code als...becoz some beginners will learn it in python....

  • @prthms_
    @prthms_ Před 7 měsíci +1

    Understood.... Thank you so much for this wonderful Video❤❤

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před rokem +1

    Most intuitive and well explained

  • @arunsagarsa3613
    @arunsagarsa3613 Před 4 měsíci

    this guy is a gem! Nothing more to say.

  • @lakshmij6067
    @lakshmij6067 Před 5 měsíci

    Understood bro, Thank you so much...Learning so much from your videos

  • @Sunil-ul1yg
    @Sunil-ul1yg Před 2 měsíci +1

    You are a legend bro!

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

    This code complexity is O(N). I understand that, this is just to reinforce BS concept, but technically this becomes linear search. I would suggest, just reject the right part of array if the critical condition arrives. ie) if( ar[mid[ == ar[low] == ar[high]) then high = mid -1, simple.. now this code is O(logn).
    For people who can't get me (coz of my bad english)..
    Here is the code ( submitted and it passed all cases ):
    while(l=arr[l]){
    if(k>=arr[l] && k=arr[m] && k

    • @jineshnadar6409
      @jineshnadar6409 Před 9 měsíci +2

      Fails for
      [1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1]
      2
      you can't say that your mid + 1 to high will not contain the target just because the arr[ low ] == arr[ mid ] == arr[ high ]
      Target can be in left half or it can be right half depending on elements and rotations you've made

    • @simarpreetsingh7235
      @simarpreetsingh7235 Před 28 dny

      ​@@jineshnadar6409exactly, his code passed because of lesser test cases

  • @muditsinghal6042
    @muditsinghal6042 Před 25 dny

    What i did was simply trim one side down in starting, for example for 1 1 1 2 2 2 1 1 1, check if nums[0] is target, if not left++ until nums[left !]= nums[right], this will make it, 2 2 1 1 1 , now do the search

    • @charchitagarwal589
      @charchitagarwal589 Před 21 dnem

      This will make the time complexity o( n )

    • @muditsinghal6042
      @muditsinghal6042 Před 21 dnem

      @@charchitagarwal589 no because we check if the array is sorted or not, only if it isn't sorted this will implement

    • @charchitagarwal589
      @charchitagarwal589 Před 21 dnem

      @@muditsinghal6042 yeah o(n) is worst case time complexity

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Před rokem

    understood
    Thank u Striver for a wonderful explanation

  • @vijaynag7723
    @vijaynag7723 Před 8 měsíci

    understood , thank you Striver.

  • @NetajiSaiSuru
    @NetajiSaiSuru Před měsícem +1

    Sir, if there was a case we need to find the first occurrance of a duplicated element in the array then what should be our approach.

    • @SimarjeetKaur-h4x
      @SimarjeetKaur-h4x Před 27 dny

      Then u have to use linear search approach as binary search won't work here..

  • @abhinav_mittal
    @abhinav_mittal Před měsícem +1

    we can just use regular binary array and return true; in place of return m; if(arr[m] == target) and in the ending we can return false; rather then return -1;

    • @abhinav_mittal
      @abhinav_mittal Před měsícem

      and i might add using sort(arr.begin(), arr.end()); function in the starting of the code

    • @umangagarwal8726
      @umangagarwal8726 Před měsícem

      ​@@abhinav_mittal why in this question we cant return index and just returning true false?

    • @abhinav_mittal
      @abhinav_mittal Před měsícem

      @@umangagarwal8726 because in question it says to return true if found and false if not found

    • @umangagarwal8726
      @umangagarwal8726 Před měsícem

      @@abhinav_mittal yeah I know that but striver says in this question you cant return index using binary search that's why I am asking why this?
      As just remove true false and return mid or -1 it will give index

    • @abhinav_mittal
      @abhinav_mittal Před měsícem

      @@umangagarwal8726 yes but I think in the interview they can ask to solve using another method so that's why

  • @brianjpeter9856
    @brianjpeter9856 Před měsícem

    Good Explanation Striver !

  • @mohithadiyal6083
    @mohithadiyal6083 Před rokem

    THE best explanation

  • @RIyaGupta-iz9iw
    @RIyaGupta-iz9iw Před 5 měsíci

    Best videos ever sir .. Understood

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

    Understood every part of the video.

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

    i followed a different approach for the special case
    if nums[low] == nums[mid] == nums[high] then i checked if mid to high is sorted
    but i checked it using linear search.

  • @anirbanghosh9611
    @anirbanghosh9611 Před rokem +2

    Instead of using if and continue can we also use a while loop for it

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

      It does not matter from a complexity standpoint.

  • @adarshagarwal9508
    @adarshagarwal9508 Před 3 měsíci

    amazing waiting for the strings sums

  • @selene8721
    @selene8721 Před 29 dny

    Thank you so much!

  • @infinitecodes
    @infinitecodes Před 10 měsíci +1

    Understood 😊

  • @paragroy5359
    @paragroy5359 Před 8 měsíci

    Great Content.
    Keep on making such videos.

  • @champbehal8111
    @champbehal8111 Před 11 měsíci +1

    Thnku striver❤

  • @David-kj7el
    @David-kj7el Před 2 měsíci

    so the only problem is in the case of (arr[low] == arr[mid] == arr[high]) for that we do nothing just do low++ and high-- (that is trimming down the search space) one of the way to handle duplicates

  • @prasannamalatesha3887

    Great video, wonderful job explaining bro!!

  • @per.seus._
    @per.seus._ Před rokem +1

    UNDERSTOOD❤

  • @ayushirastogi9725
    @ayushirastogi9725 Před rokem

    understood love the course!!!

  • @kingbadshah452
    @kingbadshah452 Před 7 měsíci

    understood everything thanks striver!!!!

  • @amitjindar6706
    @amitjindar6706 Před 26 dny

    UNDERSTOOD

  • @culeforever5408
    @culeforever5408 Před 10 měsíci +1

    understood

  • @aakashsharma780
    @aakashsharma780 Před rokem

    Understood Striver bhaiya ❤🙌

  • @amityadav-np1rk
    @amityadav-np1rk Před rokem

    Great explanation!!

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 5 měsíci

    Understood, thank you.

  • @RaghavN-rd5zw
    @RaghavN-rd5zw Před 4 měsíci

    Understood!!! Thanks striver!!!

  • @anuragprasad6116
    @anuragprasad6116 Před 7 měsíci +1

    To solve this in O(log N), we can use if (arr[mid] == arr[high]) high = mid-1;
    That is, if arr[mid]==arr[high] we can be sure that the complete right half has elements = arr[mid] and it can be safely discarded.

  • @shrirambalaji2915
    @shrirambalaji2915 Před rokem

    Understood and you are awesome brother...

  • @ashokbabug40
    @ashokbabug40 Před rokem

    Amazing video bro really appreciated

  • @dattareddy9996
    @dattareddy9996 Před 2 měsíci

    I have a doubt."The algorithm can give the index of the searched element but it cannot guarantee the least index of the searching element"isn't it correct?

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

    Understood. Thanks a lot.

  • @HappyHumbleHopefulHelpKey

    Understood ❤

  • @tanmaychaudhary2801
    @tanmaychaudhary2801 Před rokem +1

    Thank you so much bhaiya ❤😇🙏

  • @rahulpathak859
    @rahulpathak859 Před rokem

    Understood everything💯

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

    Amazing work bro, keep rocking

  • @myproject6768
    @myproject6768 Před rokem

    Absolutely understand ❤

  • @abhisarsahay8136
    @abhisarsahay8136 Před měsícem

    understood striver!!!!

  • @arjitgautam365
    @arjitgautam365 Před rokem

    well explained. Appreciated

  • @priyanshunautiyal1662
    @priyanshunautiyal1662 Před 5 měsíci

    Understood bhaiya ❤

  • @ramujanak963
    @ramujanak963 Před 9 měsíci

    yes its understood striver 💙

  • @UdaysStudy-h7d
    @UdaysStudy-h7d Před měsícem +1

    In this case
    nums=[1,2,1]
    target=1
    this code will not work, to cover this scenario, you have to add one line that checks before increment low and decrement high
    if((nums[mid]==nums[low]) && (nums[mid]==nums[high])){
    if(nums[low]==target) return true; *************************** YOU HAVE TO ADD THIS LINE ********************************
    low++; high--;
    continue;
    }

  • @ritikasingh5443
    @ritikasingh5443 Před 8 dny

    thank you sir

  • @anshikajain7990
    @anshikajain7990 Před 3 měsíci

    Understood😊😊🎉

  • @ashwinnema06
    @ashwinnema06 Před 7 měsíci

    For this solution I submitted the previous solution that you told just modified one condition nums[mid]

  • @JothiprakashThangaraj
    @JothiprakashThangaraj Před 2 měsíci

    understood!!!! thanks a lot!

  • @utsavseth6573
    @utsavseth6573 Před rokem

    Great lecture .

  • @mehrabhossain8225
    @mehrabhossain8225 Před 8 měsíci

    best explanation bro

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i Před měsícem

    When we have duplicates, we think for the solution if it was unique. Then handle the case where it fails for the duplicate one.

    • @umangagarwal8726
      @umangagarwal8726 Před měsícem

      why in this question we cant return index and just returning true false?

    • @user-ym1nv1pw8i
      @user-ym1nv1pw8i Před měsícem

      @@umangagarwal8726
      Return answer as per the Problem statement

  • @ayushlakshakar9393
    @ayushlakshakar9393 Před rokem +1

    Just a question, u said we can't find the index, we have to go linearly for that. So my question is why ? When we are returning true by comparing with arr[mid], it means at index=mid, we found that element.

    • @aniketdutta1041
      @aniketdutta1041 Před rokem +3

      since here elements are repeated...so a paticular index is not the answer.........for ex arr[]=[3,1,2,2,3,3] and target =3....what should be the index here? 0,4 or 5? we cant say anything... we simply can say wether an element 3 exists or not(true or false)

    • @abhaymandal4903
      @abhaymandal4903 Před rokem

      Even we can get the index at n/2 time complexity also by using some nice hacks

  • @nm.tech1001
    @nm.tech1001 Před měsícem

    Understood☺

  • @user-od5hg3hc3w
    @user-od5hg3hc3w Před 23 dny

    Amazing

  • @TON-108
    @TON-108 Před 10 měsíci

    Understood!

  • @abhishekverma7604
    @abhishekverma7604 Před rokem +1

    Thanks Striver..
    for lot of Duplicates everybody try out these :-
    int left = 0;
    int right = nums.size()-1;
    int mid;
    while(left

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Před 3 měsíci

    Understood !!

  • @harshdeepmaurya881
    @harshdeepmaurya881 Před rokem +1

    What about a scenario arr = [3,1,2,2,2] ?

  • @RAJSINGH-mr7hq
    @RAJSINGH-mr7hq Před rokem

    Superb. Understood!!

  • @KCOYASH
    @KCOYASH Před rokem

    UnderStood :) and thanks for such great videos ..

  • @infernogamer52
    @infernogamer52 Před rokem

    Understood Bhaiya!

  • @ritikasingh5443
    @ritikasingh5443 Před 8 dny

    understood!

  • @beginner9599
    @beginner9599 Před 3 měsíci

    understood!🙂

  • @anythingaboutrandomness

    Understood

  • @shubhamsharma-nb8yd
    @shubhamsharma-nb8yd Před rokem

    Instead of continue , you can change next if to else if ... that will also work :P

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

    Understood Striver👌

  • @abhinavanand7030
    @abhinavanand7030 Před 8 měsíci

    how can we say right half is sorted by just seeing mid element is < high element? it's also possible that high element is larger than the mid element but inside the set the elements are jumbled. similarly the case with left half @take U forward

    • @devarapallivamsi7064
      @devarapallivamsi7064 Před 7 měsíci +1

      Please take a look at how the rotation is done. I was also confused by this. but, it gets clarified if you observe carefully the input arrays.

  • @Karthik-ep6en
    @Karthik-ep6en Před 4 měsíci

    understood 👍

  • @BhavanaSree-oj7kn
    @BhavanaSree-oj7kn Před 8 měsíci

    I had done the same logic in python but the time complexity it is showing is O(N). Can you please tell me why the difference is coming in python and C++

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

    Thank u striver... ❤

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

    buddy is a god

  • @oyeesharme
    @oyeesharme Před měsícem

    understood bhaiya

  • @motivatewithme123
    @motivatewithme123 Před 28 dny

    STRIVER BHAII 💌

  • @singhji4149
    @singhji4149 Před rokem

    Nice one striver as usual

  • @Ashumaan._
    @Ashumaan._ Před 4 měsíci

    Understood !!! :)

  • @Lofisasddle
    @Lofisasddle Před 3 měsíci

    understood sir

  • @ujjawalraj6096
    @ujjawalraj6096 Před rokem

    Understood everything

  • @srushtibhagchandani7255
    @srushtibhagchandani7255 Před 2 měsíci

    understood!!!