Search Element In a Rotated Sorted Array | LeetcodeBS-4. Search Element in Rotated Sorted Array - I

Поділитися
Вставка
  • Опубліковано 19 вер 2024
  • Please watch our new video on the same topic: • BS-4. Search Element i...
    Check our Website:
    Notes: • BS-4. Search Element i...

КОМЕНТАРІ • 184

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

    Please watch our new video on the same topic: ua-cam.com/video/5qGrJbHhqFs/v-deo.html

  • @asheskhan2293
    @asheskhan2293 2 роки тому +34

    This playlist will be remembered in the history of Programming that there is someone who explains in such a way that the thing goes and fits in the middle brain and never moves to low or high part of the brain . It just remain there.

  • @meowmr9
    @meowmr9 Рік тому +13

    WTF MAN. THIS CHANNEL IS JUST GOD LEVEL IN TERMS OF EXPLANATIONS. Man I am going to donate to your channel. The best freaking channel for coding explanations on youtube.

  • @hereweare644
    @hereweare644 2 роки тому +17

    Great. I watched upto 4 videos struggling to understand. At last I found your video explained thoroughly. I'm super satisfied.

  • @NoName-ip4tt
    @NoName-ip4tt 2 роки тому +4

    This is the most lucid and intuitive explanation for this question. I managed to solve this problem watch this video only once...

  • @indrakantd7909
    @indrakantd7909 2 роки тому +10

    The explanation is so awesome. I was just getting confused in between every time while coding this question. But then, I watched only 1/3rd of your video, and YAY! I got the whole approach and got the correct submission as well. Keep making content like this Sir!

  • @Techandcarsbeaste
    @Techandcarsbeaste Рік тому +3

    Searched it on many places, but finally understood it here… thanks a lot!!

  • @dheerajvemula3457
    @dheerajvemula3457 8 місяців тому

    I have watched multiple videos but this one has the clear explanation than all the others. thank you!!

  • @takeUforward
    @takeUforward  3 роки тому +35

    Do write "understood" if you understood, motivates me :)
    Insta: instagram.com/striver_79/​
    Telegram: bit.ly/tuftelegram​

    • @Abhishek-yy3xg
      @Abhishek-yy3xg 3 роки тому

      Understood

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

      hi bro thanks for your effort
      i have 1 question, i tried both approach for linear complexity solution (iterative) i got 100% faster but for logN i got 75.51% faster in leetcode. why so? logN solution should be faster right?

    • @takeUforward
      @takeUforward  3 роки тому +8

      @@rahulpatil4749 leetcode’s runtime feature is a flaw, ignore that, and focus on tc only.

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

      @@takeUforward okay bro.

    • @GauravTiwari-fz2wm
      @GauravTiwari-fz2wm 2 роки тому

      understood

  • @Ace-ex9gg
    @Ace-ex9gg Рік тому +2

    i did two method
    method 1 : p pass ,find pivot and then do binary search
    method 2: strivers method

  • @shivamsiddharthasinghrajaw7671
    @shivamsiddharthasinghrajaw7671 2 роки тому +6

    I submitted this solution to leetcode
    surprisingly linear search runtime was 3ms and binary search runtime was 11ms

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

      I also got similar runtimes

    • @Rahul-ls4ju
      @Rahul-ls4ju Рік тому +1

      Striver has told not to believe on Leetcode runtime %ages as it depends on other factors as well

  • @honeysharma3169
    @honeysharma3169 3 роки тому +3

    mid nikalne k liye jo right shift operator ki trick use kri hai , vo bhot osm lgi
    Thanks for explaining so well

  • @karanbhati7552
    @karanbhati7552 3 роки тому +5

    I have done code by myself but after saw your code it exactly looks Same =D !!

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

      same goes for me :0

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

      @@sourabhchoudhary7289 can you help me with doubt ? : is this a valid we to solve this ques : We can also find the minimum element in the array and perform binary search on it's left and right and get the answer right ?

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

      can you help me with doubt ? : is this a valid we to solve this ques : We can also find the minimum element in the array and perform binary search on it's left and right and get the answer right ?

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

      @@freshcontent3729 ofcourse we can but to final min element O(n) then for search O(logn) which is less efficient then discussed solution

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

      @@sourabhchoudhary7289 We can actually find the min element in O(log n) time.

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

    Amazing... It's amazing!✨💯🤩
    You are my favourite teacher from now on... Striver!🥳

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

    I just discovered this channel, God bless you man. I really appreciate

  • @prajaktakapoor7520
    @prajaktakapoor7520 8 місяців тому

    Just want to say Thanksss Man!!!!

  • @Nishant89-i2z
    @Nishant89-i2z 3 роки тому +7

    Questions ka frequency badh gaya thanks bahiya

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

    Hi Striver, just one clarification, why "equals to" has been used?
    That is instead of why we use =?

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

      so that we can include extreme ends

    • @anshumaan1024
      @anshumaan1024 Рік тому +3

      yes when i remove equal to condition from line -10 ,its gives TLE, idk why

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

      @@dreamer6911 are you talking about line no: 10 ?

    • @jithuboi
      @jithuboi Рік тому +1

      @@anshumaan1024 Yea same i do that first then after seeing your comment i add that = then it accepted.

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

    Best explanation!!!

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

    how easily you just explained ... fab!!

  • @ambarkansal4823
    @ambarkansal4823 Рік тому +2

    Let's take the case:
    A[5,6,1,2,3,4] , target =6
    According to your algorithm if A[low]this condition fails,so we'll slice the left half and make low=mid+1 , now what about 6 in the left half?

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

      int binarysearch(vector &arr, int n, int x)
      {
      int low = 0;
      int high = n - 1;
      while(low

    • @adarshvishwakarma2862
      @adarshvishwakarma2862 Рік тому +2

      we are not completely ignoring left half because you see in the else block we are checking whether target lies in range or not. If it lies then only we are going with right half otherwise still going to left half

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

    on youtube everybody taking the pivot element and then doing binary search
    but strive bhaiya ,I am here we can do it with different methods as well

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

    int search(vector& nums, int target) {
    int low=0,high=nums.size()-1;
    while(low>1;
    if(nums[mid]==target) return mid;
    if(nums[low]=nums[low] && target=nums[mid] && target

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

    @take U forward
    here after the last else condition
    Do not miss to update Mid element mid = start + (end- start)/2
    otherwise its gives only -1 for each testcase

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

    Thanks striver, great explanation, after explanation I could solve by myself :)

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

    what if the mid falls at index 5 and we neglect left half because its not sorted (which contained our target =0) wouldnt that give a -1 answer
    Which is wrong?

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

      +1

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

      @@nikhilahuja9680 if left half is not sorted, then we are going to right half (which then would be sorted), But if element is not present in right half we are going on else, which means eventually we went to left half :)

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

    understood

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

    Crisp and Clear Explanation! 👌👨‍💻

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

    thanks a lot bruh i was skipping this question in frustration of not understanding bt finally i got the logic.

  • @kevinmanavalan5355
    @kevinmanavalan5355 16 годин тому

    why the equality check with mid in the "else if "block and the "else" block of the base conditions if the equality check with mid is already false in the if condition?

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

    You are brilliant teacher !!!!! ❤

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

    damm mann.. no one can take place of striver ..... understood +++++
    respect++

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

    //understood :)
    int search(vector& nums, int target) {

    int low=0;
    int high=nums.size()-1;

    while(low>1;
    if(nums[mid]==target)
    return mid;

    //check whether left half is sorted
    if(nums[low]

  • @BS-eu9do
    @BS-eu9do 2 роки тому +1

    great explanation,ever seen

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

    Amazing explanation! Keep up the good work man.

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

      can you help me with doubt ? : is this a valid we to solve this ques : We can also find the minimum element in the array and perform binary search on it's left and right and get the answer right ?

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

      @@freshcontent3729 Yes. Its a valid solution

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

      @@freshcontent3729 finding the minimum element has a worst case time complexity of O(n), but we have to solve it in O(log n).

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

      @@prasannaprabhakar1323 can be found using binary search

    • @prateekbhaisora
      @prateekbhaisora 9 місяців тому

      @@freshcontent3729 Yes it is possible. I found the largest element and then performed BS. Below is my Javascript implementation of the same:
      var search = function(nums, target) {
      const n = nums.length;
      let low = 0, high = n-1;
      let brkpoint = -1;
      while (low nums[mid-1]) &&
      (mid == n-1 || nums[mid] > nums[mid+1]))
      {
      brkpoint = mid;
      break;
      }
      else if (nums[mid] > nums[n-1])
      low = mid + 1;
      else
      high = mid - 1;
      }
      low = 0, high = n-1;
      while (low

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

    [3, 1] target = 1
    Test case not passes.

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

    if the left portion is not sorted ,is we have to sort the left pportion?
    in any other que.

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

    Hi. I have a doubt. This is my solution -
    class Solution:
    def search(self, arr: List[int], target: int) -> int:
    lo = 0
    hi = len(arr) - 1
    while (lo = arr[lo]:
    #target lies in left half
    #adjust hi to be one less than mid
    hi = mid - 1
    if target > arr[mid] and target = arr[lo]:` to `if target < arr[mid]` and change `if target > arr[mid] and target arr[mid]`
    Then the solution runs perfectly. What's going on here?

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

      bro you have not checked a condition whether the left side is sorted or not
      the condition if arr[low]

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

    Finding pivot first and do binary search pivot as mid is the better way.

  • @Rohit-fz4vd
    @Rohit-fz4vd Рік тому

    thanx a lot buddy...ur vdos are really helpful :)

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

    Thank you for the solution, very helpful😃😄

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

    Thank you so much, Striver!!!!!!!!!!!!!

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

    Hey can u tell me from where I can learn binary tree and dp

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

      Leetcode for Tree and for DP, I prefer gfg

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

      try kashish mehndiratta channel for trees

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

    @takeUforward I don't know how so many people have understood and submitted it. But the code is not giving the correct output for the input array A:[2,20,10,8] and target integer B=8. It is giving -1 but we all know answer should be 3. please help me anyone

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

    precise and clean presentation

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

    Never thought this way, thanks!

  • @Arindom-d1k
    @Arindom-d1k 3 місяці тому

    understood

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

    can you please create series that includes all the problems , can be solved using binary search

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

    why does it matter which side is sorted ?, in that example both left and right side is sorted

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

    Understood !
    But how to handle duplicate elements for the same question please tell

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

      when checking if *arr[mid] == target* , further check if these condition holds *if mid > 0 and arr[mid-1] == target* . if it holds, go left by setting *lo = mid +1* . **mid>0** ensures we have not gotten to the end of the array, which helps to avoid *index error* . *arr[mid-1] == target* check if element before the middle is not equal to target.

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

    Bhai pls upload one question daily my test is coming 😀

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

    is this two pointer solution is O(logn)???
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    low=0
    high=len(nums)-1
    while low

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

    you are the best explainer

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

    Understood. Amazing explanation as always..

  • @jansirani.t3679
    @jansirani.t3679 Рік тому

    Clear explanation 🙌

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

    Understood for life.......Bhaiya!!!!!

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

    Actually the solution doesn't work for the reversely sorted arrays

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

    Bahut hi mast padhato ho bhaiya

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

    Finally understood it!

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

    thank u for well wishes but i have headache
    luv u

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

    Greatttt!!!! Nicely explained:)))

  • @VimanshMahajan
    @VimanshMahajan 3 місяці тому

    in first inequality check why are we checking a[low]

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

    Really nice video, I was able to code by myself

  • @v.sreesairam9488
    @v.sreesairam9488 3 роки тому +2

    Bhaiya on fire 🔥

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

    Great explanation!

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

    Understood🙏!!!

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

    If we have duplicates like
    7 4
    3 3 3 3 4 3 3
    Neither left nor right side is sorted

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

    Is O(n log n) better than O(n)?

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

    Understood. Thanks a lot!

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

    Great Explanation

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

    Understood

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

    Thank you, Sir, understood it

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

    understood!

  • @KaisarAnvar
    @KaisarAnvar Рік тому +1

    This approach didn't output the correct result for some reason. It kept giving -1. Finding pivot might be a better and safest choice, but just wanted to search for other approaches like this one, but somehow it failed.

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

      bro listen for finding pivot is easy you just have to iterate through array and find the first element when which is less that 0th element but that will just increase its time complexity to nlogn isse badhiya linear search kr lete , aur lagega toh yaha binary search hi

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

      yes it gives -1

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

    Hello striver bhaiya, i am not able to the solve it's adavance question on leetcode , problem is that if same question but all the elements are not distinct.. means elements may be repeating... Like arr = (1,0,1,1,1)

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

    if(nums.size() == 1) return (nums[0] == target) - 1;
    int low = 0;
    int high = nums.size() - 1;
    int mid;
    while(low

  • @nitinverma6254
    @nitinverma6254 3 роки тому +3

    🔥🔥🔥

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

    understood!
    Thanks

  • @AyushKumar-od9zl
    @AyushKumar-od9zl 3 роки тому +1

    understood sir

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

    understood.. Thank you

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

    Understood sir

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

    What if elements are not distinct?

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

    Thank you !

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

    understood ♥️

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

    can anyone help me to find pivot element in rotated sorted array with duplicates?

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

    Test Case# 1 : arr=[3,1] target = 1 expected op: 1 but your code is failed if it is in fully rotated

    • @Steve-kv5we
      @Steve-kv5we Рік тому

      It runs perfectly fine; you must have made some mistake which I think you put ">" rather than ">=" in the first if condition.

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

    For the test case [5,1,3], target=5, code fails because a[low]>a[mid] so it goes directly to right half and hence 5 will not be found. Please reply

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

      Works fine..

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

      @@takeUforward Can u explain how it works fine? Thanks for reply

    • @takeUforward
      @takeUforward  3 роки тому +3

      @@vajayantpratik9050 the code is in the description you can use that to debug :)

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

      Right half m gaya but right half m mila nahi toh dobara left half m chala gaya

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

    OP explanation

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

    I don't think this solution will pass all the test cases, better solution will be to find the pivot element in the array and then apply binary search

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

      Yes, I checked that, this solution doesn't work for the reversely sorted arrays. Finding the pivot and applying binary search on the both side of the pivot saved me

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

      However, finding pivot takes an extra (logn).

  • @compeng..1510
    @compeng..1510 Рік тому

    Understoodddd ❤️❤️❤️

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

    Understood 💯💯💯

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

    Thanks Bhai
    Understood

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

    what if my target is on left half while left half is not sorted.

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

    understood bhaiya

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

    Thanks bhaiya

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

    Why is there not a billion likes button on this video ?

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

    Does anyone know which drawboard app striver uses??

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

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

    understooood!

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

    10 march 2022 9:43am