Search in Rotated Sorted Array

Sdílet
Vložit
  • čas přidán 11. 10. 2021
  • There is an integer array nums sorted in ascending order (with distinct values).
    Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
    Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
    You must write an algorithm with O(log n) runtime complexity.
    Example 1:
    Input: nums = [4,5,6,7,0,1,2], target = 0
    Output: 4
    Example 2:
    Input: nums = [4,5,6,7,0,1,2], target = 3
    Output: -1
    Example 3:
    Input: nums = [1], target = 0
    Output: -1

Komentáře • 24

  • @koushalnmusic
    @koushalnmusic Před rokem +6

    EVERY ELEMENT IS EITHER LEFT SORTED OR RIGHT SORTED !! this explanation made it easy for me to understand the algorithm, Thanks a lot for all the work you've been putting into making these videos.

    • @mayankmudgal1728
      @mayankmudgal1728 Před rokem

      Exactly! I have been scratching my head for hours. This is the ONLY explanation which made sense

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

    every array have 2 part either left sorted or right sorted. first we need to check it if left side part is sorted and if the target value is lies between that range that means we have to eliminate the right side otherwise we have to eliminate left side . this part is clear my whole doubt. thanks mam for making this video.

  • @tanim913
    @tanim913 Před rokem +2

    most underrated explanation, thanks 😊

  • @opsumon1569
    @opsumon1569 Před 5 měsíci +1

    Wow ! Its Very easy after this video . Thanks Di

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

    Awesome!!!
    You are beyond imagination who can convert complex things into easy one.

  • @oneandonlyflow
    @oneandonlyflow Před rokem

    one of the best explanations I've seen, thank you!

  • @poojapawar-s3i
    @poojapawar-s3i Před rokem

    thank you so much. This is the best explanation on binary search in rotated sorted array.

  • @mayankmudgal1728
    @mayankmudgal1728 Před rokem +1

    very cool!

  • @RockStar-Siblings
    @RockStar-Siblings Před rokem

    excellent explanation and ur making coding so nteresting, thank u very much

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

    Minor comment on line 19 and 31. In the condition, it doesn't need to be (....

  • @code_lover2889
    @code_lover2889 Před 2 lety

    Amazing explaination ❤️

  • @sushmitakumari8252
    @sushmitakumari8252 Před rokem

    finally i understood here. thank you so much. 🙂

  • @vaibhavsharmaiiitu9319

    Thank you really understood

  • @Rgpv_Official
    @Rgpv_Official Před rokem

    great expl

  • @kushalshukla444
    @kushalshukla444 Před rokem

    but how [1,3,5] is rotated array cause the pivote index is less than k

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

    Did you do your schooling at AECS Kalpakkam.

    • @probabilitycodingisfunis1
      @probabilitycodingisfunis1  Před 4 měsíci +1

      Yes!

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

      @@probabilitycodingisfunis1 You were of 2016 batch ..... I am junior of 2018 batch .... abhi bhi yaad hai u were the captain of the school..... very happy that ur sharing ur knowledge through utube...

    • @probabilitycodingisfunis1
      @probabilitycodingisfunis1  Před 4 měsíci +1

      @@omkarmandias8166 good old days!! AECS times were best!! thanks, wish you the best too !

  • @RishabhSharma-tp7rs
    @RishabhSharma-tp7rs Před rokem

    mam everything is right but while running it is giving error;
    int search(vector& nums, int target) {
    int left =0;
    int right =nums.size()-1;
    int mid=left+(right-left)/2;
    while(left

  • @kushalshukla444
    @kushalshukla444 Před rokem

    nice exp :)