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

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024

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

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

    Please watch our new video on the same topic: th-cam.com/video/5qGrJbHhqFs/w-d-xo.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.

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

    Just want to say Thanksss Man!!!!

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

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

  • @kevinmanavalan5355
    @kevinmanavalan5355 วันที่ผ่านมา

    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?

  • @Arindom-d1k
    @Arindom-d1k 3 หลายเดือนก่อน

    understood

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

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

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

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

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

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

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

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

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

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

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

  • @Rohit-fz4vd
    @Rohit-fz4vd ปีที่แล้ว

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

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

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

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

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

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

  • @deepkodes4434
    @deepkodes4434 2 ปีที่แล้ว

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

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

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

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

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

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

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

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

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

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

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

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

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

      No , O(n) is better

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

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

  • @stratonov
    @stratonov 2 ปีที่แล้ว

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

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

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

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

    10 march 2022 9:43am

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

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

    understood

  • @Nishant89-i2z
    @Nishant89-i2z 3 ปีที่แล้ว +7

    Questions ka frequency badh gaya thanks bahiya

  • @rvaisakh10
    @rvaisakh10 2 ปีที่แล้ว

    thank u for well wishes but i have headache
    luv u

  • @krushnapatil9196
    @krushnapatil9196 2 ปีที่แล้ว

    1:46

  • @prithviamruthraj
    @prithviamruthraj 2 ปีที่แล้ว

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

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

    how easily you just explained ... fab!!

  • @dreamer6911
    @dreamer6911 2 ปีที่แล้ว

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

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

    Understood sir

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

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

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

    understood ♥️

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

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

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

  • @divyansh2212
    @divyansh2212 2 ปีที่แล้ว

    understood bhaiya

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

    Best explanation!!!

  • @BS-eu9do
    @BS-eu9do 2 ปีที่แล้ว +1

    great explanation,ever seen

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

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

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

  • @shreyashachoudhary480
    @shreyashachoudhary480 2 ปีที่แล้ว

    Never thought this way, thanks!

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

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

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

    OP explanation

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

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

    ❤❤

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

    Understood 💯💯💯

  • @compeng..1510
    @compeng..1510 ปีที่แล้ว

    Understoodddd ❤️❤️❤️

  • @devbhattacharya153
    @devbhattacharya153 2 ปีที่แล้ว

    Thanks bhaiya

  • @ganavin3423
    @ganavin3423 2 ปีที่แล้ว

    Thanks Bhai
    Understood

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

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

    Crisp and Clear Explanation! 👌👨‍💻

  • @bhaveshkumar6842
    @bhaveshkumar6842 2 ปีที่แล้ว

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

  • @jatinbhuker7929
    @jatinbhuker7929 2 ปีที่แล้ว

    can anyone explain me this line => int mid = (low + high) >> 1;

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

      1. ( x >> 1 ) == ( x / 2 )
      right shift by 1 bit equivalent to division by 2
      2. ( x

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

      it same as int mid= (low+high)/2

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

    What if elements are not distinct?

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

  • @suryansh70
    @suryansh70 2 ปีที่แล้ว

    you are the best explainer

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

    Bhai pls upload one question daily my test is coming 😀

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

    this c++ code is giving tle in leetcode...

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

      brother use equal to condition also in else if, and else statement. Idk why its not working without it

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

    Understood. Amazing explanation as always..

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

    Bahut hi mast padhato ho bhaiya

  • @Dipubuet
    @Dipubuet 2 ปีที่แล้ว

    precise and clean presentation

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

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

  • @jansirani.t3679
    @jansirani.t3679 ปีที่แล้ว

    Clear explanation 🙌

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

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

    You are brilliant teacher !!!!! ❤

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

    Really nice video, I was able to code by myself

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

  • @dawodujohnson
    @dawodujohnson 2 ปีที่แล้ว

    Finally understood it!

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

    #DSAGangster

  • @vatsalkudecha2746
    @vatsalkudecha2746 3 ปีที่แล้ว

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

  • @abhisheksoni9644
    @abhisheksoni9644 2 ปีที่แล้ว

    Thank you for the solution, very helpful😃😄

  • @AryanChaudhary-b3k
    @AryanChaudhary-b3k ปีที่แล้ว

    can't we just fi d the peak element and then perform BS on two subarrays.

  • @rhythmbhatia8906
    @rhythmbhatia8906 2 ปีที่แล้ว

    Incomplete Explanation! What if a[low]>=a[mid] is not explained!

    • @Steve-kv5we
      @Steve-kv5we ปีที่แล้ว

      That's what the else condition is for🙃.

  • @Geetli28
    @Geetli28 2 ปีที่แล้ว

    Great 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).

  • @shivalikagupta3433
    @shivalikagupta3433 2 ปีที่แล้ว

    Greatttt!!!! Nicely explained:)))

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

  • @v.sreesairam9488
    @v.sreesairam9488 3 ปีที่แล้ว +2

    Bhaiya on fire 🔥

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

    Understood

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

    Waah waahgreat

  • @Shadow-lx9rh
    @Shadow-lx9rh 2 ปีที่แล้ว

    This code is not working for array
    1 0 1 1 1
    Target = 0 😭 in leetcode

  • @mayankraj9004
    @mayankraj9004 3 ปีที่แล้ว

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

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

    🔥🔥🔥

  • @shubhankarsharma154
    @shubhankarsharma154 2 ปีที่แล้ว

    understood.. Thank you

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

    understood!

  • @AyushKumar-od9zl
    @AyushKumar-od9zl 3 ปีที่แล้ว +1

    understood sir