First and Last Position of Element Sorted Array (LeetCode 34) | Full Solution | Study Algorithms

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • You are gIven an array of integers that is sorted in an ascending order and a target value. Find the index of first and last occurrence of the element in a fastest way possible. The desired complexity is less than linear time. Watch the video to learn how we can derive a clever solution using the concept of binary search. Work along with me to see how it can be solved easily along with the dry-run of code in JAVA.
    Chapters:
    00:00 - Intro
    01:21 - Problem statement and description
    03:12 - Brute Force Solution
    06:03 - Efficient approach
    07:44 - Using Binary Search Twice
    10:30 - Dry-run of Code
    14:26 - Final Thoughts
    📚 Links to topics I talk about in the video:
    Binary Search: • Binary Search Algorith...
    Divide and Conquer: • Divide and Conquer alg...
    Time Complexity: • What is the Time Compl...
    What is Big O?: • Big O Notation Simplif...
    LeetCode Problems: • Leetcode Solutions
    📘 A text based explanation is available at: studyalgorithms.com/
    Problem on LeetCode: leetcode.com/problems/find-fi...
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/36pU0JO
    Favorite book to understand algorithms: amzn.to/39w3YLS
    Favorite book for data structures: amzn.to/3oAVBTk
    Get started for interview preparation: amzn.to/39ysbkJ
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    🎥 My Recording Gear:
    Recording Light: amzn.to/3pAqh8O
    Microphone: amzn.to/2MCX7qU
    Recording Camera: amzn.to/3alg9Ky
    Tablet to sketch and draw: amzn.to/3pM6Bi4
    Surface Pen: amzn.to/3pv6tTs
    Laptop to edit videos: amzn.to/2LYpMqn
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #interview

Komentáře • 22

  • @HarshChauhan-ur4jy
    @HarshChauhan-ur4jy Před 5 dny

    I watched two explanations before this but couldn’t understand them. However, your teaching method was awesome, and I was able to write the code on my own right after understanding your solution. Thank you!

  • @MVCSPratyushKumarShekhar
    @MVCSPratyushKumarShekhar Před 2 lety +10

    This is really one of the most underrated video on CZcams. The way you have explained this concept is awesome. Please do continue this good work. Everyone can be a good programmer/coder but everyone can't be a good teacher.

    • @nikoo28
      @nikoo28  Před 2 lety +3

      Thanks for all the love and support

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

    I just started learning DSA when i am unable to solve leet code problem i first search in your youtube channel for a solution, In my opinion you are best at explain algorithms, Keep on going .

  • @sohitmaurya4782
    @sohitmaurya4782 Před 2 lety +1

    Great Explanation👏👏👏

  • @atifraza9960
    @atifraza9960 Před 2 lety

    The way of your explanation excellent 👏👏👏

  • @aarzoo2302
    @aarzoo2302 Před 9 měsíci +1

    u explain everything and it was quite easy to understand, thankyouuu

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

    The way you make things clear is superr

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

    the way of teaching makes us to solve the more problems

  • @mikedelta658
    @mikedelta658 Před 6 měsíci +1

    Fantastic and clear explanation. Thank you!

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

      You're very welcome!

  • @rambhaktuchihaobito7987

    Thank you sir ❤

  • @casstansa
    @casstansa Před 24 dny

    brilliant🤩

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

    Will this solution work if input is [1,2,3] and target is 2

  • @madhumithraravi9087
    @madhumithraravi9087 Před 2 lety

    What happens if the first and the last index both exists in left bound? or right bound? this approach will return last index or first index as -1 right?

    • @nikoo28
      @nikoo28  Před 2 lety +2

      Let’s say both first and last exist in the left bound.
      When you first divide the array, you will not find the element in the middle, so following a binary search algorithm you will look in the left half. This will be your new array now.
      So you will again find the first and last index in the left half.
      Try out a test case on your own and you will understand it better :)

    • @madhumithraravi9087
      @madhumithraravi9087 Před 2 lety

      @@nikoo28 Kind of recursive

    • @nikoo28
      @nikoo28  Před 2 lety

      Yes indeed…this is a recursive solution. Just like binary search.

    • @madhumithraravi9087
      @madhumithraravi9087 Před 2 lety

      @@nikoo28 thank you

    • @fero___3005
      @fero___3005 Před 2 lety

      @@nikoo28 when low < high breaks the loop and the low value = 3 but the index value is still 2 how will you get 3 as output plz explain sir

  • @KUNALPRAJAPATI-fi3jy
    @KUNALPRAJAPATI-fi3jy Před 4 měsíci

    just love the explanation