BS-5. Search Element in Rotated Sorted Array II

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ต.ค. 2024

ความคิดเห็น • 378

  • @takeUforward
    @takeUforward  ปีที่แล้ว +71

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

    • @FactFactoryCentral
      @FactFactoryCentral ปีที่แล้ว +8

      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 ปีที่แล้ว

      understood

    • @venup2813
      @venup2813 ปีที่แล้ว

      Understood

    • @abhisheksinghmehra9576
      @abhisheksinghmehra9576 ปีที่แล้ว

      Understood

    • @365tage9
      @365tage9 9 หลายเดือนก่อน

      Understood. You are a genius.

  • @hardikpatel352
    @hardikpatel352 6 หลายเดือนก่อน +18

    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 10 หลายเดือนก่อน +20

    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.

  • @Mr.Manoj1
    @Mr.Manoj1 12 วันที่ผ่านมา +1

    Understood! You are a savior to people who struggle in DSA (im one of them) and I cant thank you enough.

  • @LearnwithEase20
    @LearnwithEase20 9 หลายเดือนก่อน +3

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

  • @reshmah1497
    @reshmah1497 3 วันที่ผ่านมา

    Really understood the concept. Thanks much for teaching very well.

  • @mehulthuletiya497
    @mehulthuletiya497 ปีที่แล้ว +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

  • @depavathnareshnaik5584
    @depavathnareshnaik5584 5 หลายเดือนก่อน

    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 ปีที่แล้ว +2

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

  • @stith_pragya
    @stith_pragya 9 หลายเดือนก่อน +1

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

  • @darshilmodi6851
    @darshilmodi6851 4 หลายเดือนก่อน +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 4 หลายเดือนก่อน

      ur modification isnt working for ur test case bro

    • @srujangajjala2658
      @srujangajjala2658 4 หลายเดือนก่อน

      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 3 หลายเดือนก่อน

      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 3 หลายเดือนก่อน

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

    • @monishamadu
      @monishamadu 2 หลายเดือนก่อน

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

  • @venkatsaireddy1412
    @venkatsaireddy1412 ปีที่แล้ว +3

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

  • @prthms_
    @prthms_ 9 หลายเดือนก่อน +1

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

  • @cinime
    @cinime ปีที่แล้ว +2

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

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr ปีที่แล้ว +2

    Understood the intuition and approach. Thanks for the series.

  • @VikashKumar-tg3ot
    @VikashKumar-tg3ot 5 หลายเดือนก่อน

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

  • @prathambhushan4859
    @prathambhushan4859 3 หลายเดือนก่อน

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

  • @aakashsharma780
    @aakashsharma780 หลายเดือนก่อน

    Again Understood Striver bhaiya ❤🔥🔥 , doing revision for placements..!!

  • @JK-de2gh
    @JK-de2gh 3 หลายเดือนก่อน +1

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

  • @AppaniMadhavi
    @AppaniMadhavi 8 หลายเดือนก่อน +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 5 หลายเดือนก่อน

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

  • @lshuarckyma
    @lshuarckyma 8 วันที่ผ่านมา +2

    TIP BY STRIVER AT THE END: if you get questions envolving duplicates then try to solve them as unique element based and modify the code where the condition fails , for ex here it breaks at identifying the sorting portion

  • @akbunofficial1281
    @akbunofficial1281 ปีที่แล้ว +3

    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 2 หลายเดือนก่อน

      Really? Can you explain with an example?

    • @djinnGamingLive
      @djinnGamingLive หลายเดือนก่อน

      I don't think that will work. Even if we check the right sorted list first we need to know about the order of the first, middle and last element before that so we can rule out the possibility of them being equal.

  • @lakshmij6067
    @lakshmij6067 7 หลายเดือนก่อน

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

  • @arunsagarsa3613
    @arunsagarsa3613 5 หลายเดือนก่อน

    this guy is a gem! Nothing more to say.

  • @vijaynag7723
    @vijaynag7723 9 หลายเดือนก่อน

    understood , thank you Striver.

  • @AtulKumar-c4x7l
    @AtulKumar-c4x7l ปีที่แล้ว

    understood
    Thank u Striver for a wonderful explanation

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 ปีที่แล้ว +1

    Most intuitive and well explained

  • @adarshagarwal9508
    @adarshagarwal9508 5 หลายเดือนก่อน

    amazing waiting for the strings sums

  • @paragroy5359
    @paragroy5359 10 หลายเดือนก่อน

    Great Content.
    Keep on making such videos.

  • @brianjpeter9856
    @brianjpeter9856 3 หลายเดือนก่อน

    Good Explanation Striver !

  • @Dipanshutripathi2407
    @Dipanshutripathi2407 ปีที่แล้ว

    Understood every part of the video.

  • @nishanthcodes4576
    @nishanthcodes4576 ปีที่แล้ว

    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 11 หลายเดือนก่อน +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 2 หลายเดือนก่อน

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

  • @kingbadshah452
    @kingbadshah452 9 หลายเดือนก่อน

    understood everything thanks striver!!!!

  • @aakashsharma780
    @aakashsharma780 ปีที่แล้ว

    Understood Striver bhaiya ❤🙌

  • @Sunil-ul1yg
    @Sunil-ul1yg 3 หลายเดือนก่อน +1

    You are a legend bro!

  • @prasannamalatesha3887
    @prasannamalatesha3887 ปีที่แล้ว

    Great video, wonderful job explaining bro!!

  • @mohithadiyal6083
    @mohithadiyal6083 ปีที่แล้ว

    THE best explanation

  • @manishchahar9110
    @manishchahar9110 8 หลายเดือนก่อน

    Amazing work bro, keep rocking

  • @ashokbabug40
    @ashokbabug40 ปีที่แล้ว

    Amazing video bro really appreciated

  • @abhinav_mittal
    @abhinav_mittal 3 หลายเดือนก่อน +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 3 หลายเดือนก่อน

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

    • @umangagarwal8726
      @umangagarwal8726 3 หลายเดือนก่อน

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

    • @abhinav_mittal
      @abhinav_mittal 3 หลายเดือนก่อน

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

    • @umangagarwal8726
      @umangagarwal8726 3 หลายเดือนก่อน

      @@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 3 หลายเดือนก่อน

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

  • @RIyaGupta-iz9iw
    @RIyaGupta-iz9iw 6 หลายเดือนก่อน

    Best videos ever sir .. Understood

  • @champbehal8111
    @champbehal8111 ปีที่แล้ว +1

    Thnku striver❤

  • @shrirambalaji2915
    @shrirambalaji2915 ปีที่แล้ว

    Understood and you are awesome brother...

  • @KR_Technical-hj3bf
    @KR_Technical-hj3bf 23 วันที่ผ่านมา

    i can say one thing about you that no one dislike you

  • @mehrabhossain8225
    @mehrabhossain8225 10 หลายเดือนก่อน

    best explanation bro

  • @sahulraj9536
    @sahulraj9536 8 หลายเดือนก่อน

    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.

  • @chirag71269
    @chirag71269 22 วันที่ผ่านมา

    Understood Striver ❤

  • @priyanshunautiyal1662
    @priyanshunautiyal1662 7 หลายเดือนก่อน

    Understood bhaiya ❤

  • @anirbanghosh9611
    @anirbanghosh9611 ปีที่แล้ว +2

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

    • @nikhilneela
      @nikhilneela ปีที่แล้ว

      It does not matter from a complexity standpoint.

  • @ayushirastogi9725
    @ayushirastogi9725 ปีที่แล้ว

    understood love the course!!!

  • @NetajiSaiSuru
    @NetajiSaiSuru 2 หลายเดือนก่อน +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.

    • @explore_with_sim
      @explore_with_sim 2 หลายเดือนก่อน

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

    • @vamshik9472
      @vamshik9472 18 วันที่ผ่านมา

      ​@@explore_with_simbs works bro use lower bound and upperbound

  • @Integral_MC
    @Integral_MC 15 วันที่ผ่านมา

    understood, thanks

  • @RaghavN-rd5zw
    @RaghavN-rd5zw 5 หลายเดือนก่อน

    Understood!!! Thanks striver!!!

  • @ramujanak963
    @ramujanak963 11 หลายเดือนก่อน

    yes its understood striver 💙

  • @selene8721
    @selene8721 2 หลายเดือนก่อน

    Thank you so much!

  • @EC20022ELAKKIYAC
    @EC20022ELAKKIYAC 4 หลายเดือนก่อน

    Understood , Thank you

  • @samuelfrank1369
    @samuelfrank1369 ปีที่แล้ว

    Understood. Thanks a lot.

  • @RaunitJaiswal-s9v
    @RaunitJaiswal-s9v หลายเดือนก่อน

    You know what life is too unpredictable not for everyone but for few it is

  • @infernogamer52
    @infernogamer52 ปีที่แล้ว

    Understood Bhaiya!

  • @arjitgautam365
    @arjitgautam365 ปีที่แล้ว

    well explained. Appreciated

  • @tanmaychaudhary2801
    @tanmaychaudhary2801 ปีที่แล้ว +1

    Thank you so much bhaiya ❤😇🙏

  • @-ShangsitNath
    @-ShangsitNath 6 หลายเดือนก่อน +2

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

    • @kushwanthkapa2041
      @kushwanthkapa2041 4 หลายเดือนก่อน

      Yup😊

    • @manusklm1161
      @manusklm1161 2 หลายเดือนก่อน

      Nope...it's n/2

    • @EerieEntertainment-mc4ce
      @EerieEntertainment-mc4ce 2 หลายเดือนก่อน

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

    • @manusklm1161
      @manusklm1161 หลายเดือนก่อน

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

  • @rahulpathak859
    @rahulpathak859 ปีที่แล้ว

    Understood everything💯

  • @KCOYASH
    @KCOYASH ปีที่แล้ว

    UnderStood :) and thanks for such great videos ..

  • @amityadav-np1rk
    @amityadav-np1rk ปีที่แล้ว

    Great explanation!!

  • @infinitecodes
    @infinitecodes ปีที่แล้ว +1

    Understood 😊

  • @creativefacts143
    @creativefacts143 8 หลายเดือนก่อน

    Understood Striver👌

  • @nagavedareddy5891
    @nagavedareddy5891 ปีที่แล้ว

    Thank u striver... ❤

  • @David-kj7el
    @David-kj7el 4 หลายเดือนก่อน

    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

  • @per.seus._
    @per.seus._ ปีที่แล้ว +1

    UNDERSTOOD❤

  • @JothiprakashThangaraj
    @JothiprakashThangaraj 3 หลายเดือนก่อน

    understood!!!! thanks a lot!

  • @muditsinghal6042
    @muditsinghal6042 2 หลายเดือนก่อน

    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 2 หลายเดือนก่อน

      This will make the time complexity o( n )

    • @muditsinghal6042
      @muditsinghal6042 2 หลายเดือนก่อน

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

    • @charchitagarwal589
      @charchitagarwal589 2 หลายเดือนก่อน

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

  • @myproject6768
    @myproject6768 ปีที่แล้ว

    Absolutely understand ❤

  • @abhisarsahay8136
    @abhisarsahay8136 2 หลายเดือนก่อน

    understood striver!!!!

  • @culeforever5408
    @culeforever5408 ปีที่แล้ว +1

    understood

  • @Liftedmonk
    @Liftedmonk 8 หลายเดือนก่อน

    buddy is a god

  • @oyeesharme
    @oyeesharme 2 หลายเดือนก่อน

    understood bhaiya

  • @utsavseth6573
    @utsavseth6573 ปีที่แล้ว

    Great lecture .

  • @prachigoyal2510
    @prachigoyal2510 ปีที่แล้ว

    No one explains like you do Striver

  • @ujjawalraj6096
    @ujjawalraj6096 ปีที่แล้ว

    Understood everything

  • @mohitsingh13
    @mohitsingh13 2 หลายเดือนก่อน

    Understood ❤

  • @ayushlakshakar9393
    @ayushlakshakar9393 ปีที่แล้ว +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 ปีที่แล้ว +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 ปีที่แล้ว

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

  • @RAJSINGH-mr7hq
    @RAJSINGH-mr7hq ปีที่แล้ว

    Superb. Understood!!

  • @ritikasingh5443
    @ritikasingh5443 หลายเดือนก่อน

    thank you sir

  • @armankhan1500
    @armankhan1500 หลายเดือนก่อน

    Striver is a real deal ...OG

  • @dattareddy9996
    @dattareddy9996 4 หลายเดือนก่อน

    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?

  • @CodeMode9313
    @CodeMode9313 ปีที่แล้ว

    shukriya habibi

  • @pratibhathakur3094
    @pratibhathakur3094 21 วันที่ผ่านมา

    why can't we return index of first or last occurence here by using binary search?

  • @faizanahmedsiddiqui5703
    @faizanahmedsiddiqui5703 หลายเดือนก่อน

    Not writing continue will also do the work!

  • @amitjindar6706
    @amitjindar6706 2 หลายเดือนก่อน

    UNDERSTOOD

  • @ashwinnema06
    @ashwinnema06 9 หลายเดือนก่อน

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

  • @RaviKumar-sn6tu
    @RaviKumar-sn6tu 6 หลายเดือนก่อน

    Understood sir ...

  • @YourCodeVerse
    @YourCodeVerse 10 หลายเดือนก่อน

    Understood✅🔥🔥

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 8 หลายเดือนก่อน

    Thank you bhaiya

  • @ESkp15
    @ESkp15 10 หลายเดือนก่อน

    Wow! Understood!

  • @akilesh6379
    @akilesh6379 29 วันที่ผ่านมา

    understood sir

  • @samarthpai5359
    @samarthpai5359 13 วันที่ผ่านมา

    Understood!

  • @singhji4149
    @singhji4149 ปีที่แล้ว

    Nice one striver as usual

  • @Coding-n8v
    @Coding-n8v ปีที่แล้ว +1

    Understood

  • @shubhamsharma-nb8yd
    @shubhamsharma-nb8yd ปีที่แล้ว

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