BS-8. Single Element in Sorted Array

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • Problem Link: bit.ly/42KKHj5
    Notes/C++/Java/Python codes: takeuforward.org/data-structu...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course

Komentáře • 285

  • @takeUforward
    @takeUforward  Před rokem +133

    The brute force can be better by just doing a XOR, but the reason we did that brute was to understand the binary search approach!

    • @sagarpatle461
      @sagarpatle461 Před rokem +4

      Outstanding 😊

    • @sudhanshushekhar4222
      @sudhanshushekhar4222 Před rokem

      Understood

    • @DeepakKumar-uq9js
      @DeepakKumar-uq9js Před rokem +1

      Thanks bhai Ji😃

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

      Will not the time Complexity differ? Brute force by xor will be O(n) where as this will be O(logn). Or are you with respect to the conditions that are being checked for logn times will be equivalent to O(n) time Complexity?

    • @gauravpandey-fx9hk
      @gauravpandey-fx9hk Před 6 měsíci +3

      Brute force Simplified:
      int n = nums.length;
      if(n == 1) return nums[0];
      // O(n/2)
      for(int i = 1;i

  • @yashmundada2483
    @yashmundada2483 Před měsícem +11

    In short,
    If I'm on (even, odd), the element occurs after me, so eliminate everything before me (the left half)
    If I'm on (odd, even), the element occurred before me, so eliminate everything after me (the right half)
    Great video as always!

  • @ratnadeepsaha7675
    @ratnadeepsaha7675 Před rokem +61

    8 video at once. U r a legend for the community. Salute.

  • @Harshitha45-sde
    @Harshitha45-sde Před 10 hodinami +1

    weekend 27 July 2024 - Streak-1
    I previously studied the binary search concept. I've now started Striver's playlist and just completed the first eight videos. Let's keep going!

  • @lakshyagupta9435
    @lakshyagupta9435 Před 8 měsíci +6

    The way you explained the approach is just awesome.

  • @AdityaKumar-be7hx
    @AdityaKumar-be7hx Před rokem +24

    Finished all 8 videos Striver :) When can we get rest of the videos?
    Thanks for putting in so much of effort to make these awesome DSA playlists available for free. All these (graph, DP, trie, tree, recursion, etc) are truly the best.

  • @adityanigam8373
    @adityanigam8373 Před rokem +6

    Striver you are the best, you clear even the smallest doubt, I always had a doubt to whether to take low< high or low

  • @cinime
    @cinime Před rokem

    Understood! So amazing explanation as always, thank you very very much for your effort!!

  • @raghavmanish24
    @raghavmanish24 Před 5 měsíci +2

    it's obvious sir , how one can not understand this simplest explanation.......thankuu so much sir

  • @YogeshKumar-px5bd
    @YogeshKumar-px5bd Před rokem +4

    The solution is great. More focussed on writing clean and readable code but not so much intuitive at first.

  • @arijitroy8652
    @arijitroy8652 Před 5 měsíci +3

    We can write the same for loop loop ie. for(i=1; i

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

    Thanks. Great way of explaining complex questions.

  • @charan123rams3
    @charan123rams3 Před rokem +1

    Sir your really great and inspiring us to learn more about the coding,thank you so much 😢

  • @user-sd1ou1db1p
    @user-sd1ou1db1p Před 11 měsíci +2

    Another way to implement this without reducing the search space would be to use the condition (r>l+1), this way you are always ensuring that the search space is atleast of size 3. So now in the end, your anwer would be either arr[l] or arr[r] and you can check for both of them. Also you'll have to place a check for when the array size is 1. By using this technique, you won't have to write code for edge case and also you don't have to think about reducing search space. Although striver's approach of reducing search space was also amazing.

  • @chphanisimha7802
    @chphanisimha7802 Před rokem

    Such an incredible work!!

  • @bhupendramaurya6587
    @bhupendramaurya6587 Před rokem

    bhaiya, you are explaining the concept too good, Thank you so much.

  • @sayakghosh5104
    @sayakghosh5104 Před rokem +1

    Mind blowing explanation.

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

    Great video help me a lot I can't explain how much help it Thank you, sir.

  • @Shunya_Advait
    @Shunya_Advait Před 11 měsíci +1

    Understood Sir, thanks a lot for this amazing video.

  • @harshsolanki1058
    @harshsolanki1058 Před 10 měsíci +5

    Two pointer method also gets the code done in O(log - base 2 - n).
    Keeping pointers low=0 and high=n-1 and doing simultaneous search and increasing or decreasing pointers by 2
    @takeUforward

    • @VasanthChoudary-uc5cz
      @VasanthChoudary-uc5cz Před 8 měsíci +2

      would'nt it take o(n/2)??

    • @shibainu7500
      @shibainu7500 Před 3 měsíci

      Two pointer is O(N) because you are traversing each element atleast once even though the number of iterations are n/2
      In binary search, we completely reject half of the search space and that's why it is O(logN)

  • @ujjawal_
    @ujjawal_ Před rokem +14

    00:00 Problem Explanation
    02:42 Bruteforce (Approach 1)
    04:52 Edge Cases
    05:45 Binary Search (Approach 2)
    20:27 Code

  • @AbhishekSingh-cq2jx
    @AbhishekSingh-cq2jx Před 10 měsíci

    understood better than Scaler paid cource really Thank You

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

    Thank you Striver...Understood everything

  • @shivajirao999
    @shivajirao999 Před měsícem

    wrote the code in one go, without any error, all the test cases passed, the satisfaction level is insane

    • @sayanpradhan1366
      @sayanpradhan1366 Před 24 dny +1

      apne is question ka logic khud banaya tha ya Striver bhaiyya ka video dekhne ke bad kiya tha ?

  • @bapanapallihemanth705

    You are the king 👑 of coding striver

  • @baderajesh23
    @baderajesh23 Před 2 měsíci +1

    Eliminating left or right part based on even,odd logic is awesome :)

  • @dayashankarlakhotia4943

    Good explanation with dry run understood

  • @prathmeshparab7046
    @prathmeshparab7046 Před rokem

    Amazing explanation ❣❣

  • @chiragsharma8905
    @chiragsharma8905 Před 9 měsíci +2

    00:06 Find the single element in a sorted array.
    02:25 Identifying the single element in a sorted array using Brute Force
    04:53 Apply binary search to optimize the code
    07:16 Identifying the half and location of the single element.
    09:30 Write a lot of edge cases and eliminate conditional statements
    11:45 Performing binary search to find the single element in a sorted array
    14:04 Identify if you are on the left half or the right half and eliminate accordingly.
    16:19 Binary Search to find the single element in a sorted array.
    18:42 In binary search, we eliminate the left or right half of the search space based on whether we are standing at an odd or even index.
    20:42 The main focus of this video is on code readability, consistent use of variables, and understanding the concept of elimination in binary search.
    Crafted by Merlin AI.

  • @bhavyasrikanchi5753
    @bhavyasrikanchi5753 Před rokem

    excellenttttt explaination..!!!!! Understood

  • @58harshverma57
    @58harshverma57 Před rokem +1

    Quite interesting question!

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

    its really great series ,Thanks Striver. Aap nhi hote to humara kya hota !!!!!!!!!!!!!!!!

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

    superb explanation

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

    Striver bhai maza hi aa gaya ...............one request to you is plzz bhai upload video alternate day😍

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

    thanks striver understood everything

  • @samuelfrank1369
    @samuelfrank1369 Před 9 měsíci

    Understood. Thanks a lot.

  • @tech1hutz
    @tech1hutz Před rokem +2

    Really Great explanation bhaiya ❤ pls can you also make a series for greedy algo questions and its approach obviously after completing this ongoing binary search series.....Its much needed coz your way of explaining approaching a problem really helps in building concepts as well as clear understanding of any problem.Thank you so much for all the series you made ...

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 3 měsíci

    Understood, thank you.

  • @ashishranjan8350
    @ashishranjan8350 Před rokem +4

    Hey striver please upload rest of the videos in this series.

  • @arpitamanderna9111
    @arpitamanderna9111 Před rokem +1

    Hey striver !! please upload rest of the videos too!! you are the best!!

  • @TrendyGamer007
    @TrendyGamer007 Před rokem +4

    @takeUforward @striver please upload the remaining binary search videos as most of us have already finished watching all 8 videos … and the content was superb 👍🏻👍🏻👍🏻.

  • @abhishekkumar-fe8lw
    @abhishekkumar-fe8lw Před 10 měsíci

    We can also trim search further by putting left=2 , and right =n-3.

  • @jeehub041
    @jeehub041 Před rokem +1

    Sir series me maja aa raha .... Ab aage ka videos bhi upload kar do please🙏🙏🙏

  • @vigneshwaran_2002
    @vigneshwaran_2002 Před rokem

    Thankyou sir very helpful❤

  • @senseiAree
    @senseiAree Před 9 měsíci +2

    I'm sorry for not being able to continue for some days. I had additional workload at my office which halted my learning curve but I made sure my daily streak is maintained in Leetcode and Coding Ninjas.

  • @PankajSingh-kz7or
    @PankajSingh-kz7or Před 11 dny

    we can also elimate a particular half on the basis of size of the array because single element will always be present in odd size array

  • @YourCodeVerse
    @YourCodeVerse Před 7 měsíci

    Understood✅🔥🔥

  • @inderjeet09
    @inderjeet09 Před rokem +1

    Please bro make a playlist on bit manipulation also thats a very difficult topic for us . Only you can make that easy.

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

    Understood ❤🎉

  • @utsavseth6573
    @utsavseth6573 Před rokem

    Shandaar.

  • @sunnypunia6485
    @sunnypunia6485 Před rokem

    bhaiya please complete all lectures of all questions in a to z dsa as soon as possible it will be very helpful

  • @aakashsharma780
    @aakashsharma780 Před rokem

    Understood @striver ❤🙌🥳

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

    Understood sir 😇😊

  • @divyadwivedi1527
    @divyadwivedi1527 Před rokem

    Plz upload rest of the video as soon as possible

  • @chethanprabhu4475
    @chethanprabhu4475 Před 21 dnem

    I think we can consider low = 0 and high = arr.length - 1. Always there will be mininum 3 elements in array and hence mid can never be equal to low or high.

  • @traymk
    @traymk Před rokem

    Kya mast thumbnail hai.. 🔥

  • @RAJSINGH-mr7hq
    @RAJSINGH-mr7hq Před rokem

    Superb 👌

  • @kiranmoura2974
    @kiranmoura2974 Před rokem

    Understood ❤

  • @hallupandet228
    @hallupandet228 Před rokem

    Understood 🤗

  • @VasanthChoudary-uc5cz
    @VasanthChoudary-uc5cz Před 8 měsíci

    you can also optimise the brute force by using two pointer technique.

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

    Excellent

  • @abhay9994
    @abhay9994 Před 9 měsíci

    Thank You.

  • @user-or5oz1pk2x
    @user-or5oz1pk2x Před 3 měsíci

    Thanks a lot Bhaiya

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

    another solution is take xor of all the elements, TC ---> O(N), SC = O(1)

  • @adarshkumarrao3478
    @adarshkumarrao3478 Před rokem +1

    UNDERSTOOD

  • @khalasianiket816
    @khalasianiket816 Před 29 dny

    understood ❤

  • @UserUser-tn8tv
    @UserUser-tn8tv Před 6 měsíci

    Understood!

  • @rakshanaravindran9809
    @rakshanaravindran9809 Před 10 měsíci

    Understood!!

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

    understood!!

  • @AYJ959
    @AYJ959 Před rokem

    Simple O(n) in java
    return arr.stream().reduce(0, (a,b) -> (a^b));

  • @pihus3498
    @pihus3498 Před rokem

    Understoooood

  • @ashwath1914
    @ashwath1914 Před rokem +3

    If we start with low = 0, high = n-1, the edge cases should be written inside and the code will look like this:
    int singleNonDuplicate(vector& arr) {
    int n = arr.size();
    int l = 0, h = n-1, ans = -1;
    if(n == 1) return arr[0];
    while(l

  • @rajeswarichalamcherla2860

    Understood !!!!

  • @adityarajsrivastava3291
    @adityarajsrivastava3291 Před měsícem

    understood!

  • @mbm.editzz
    @mbm.editzz Před rokem

    thank you sir

  • @sulthan6131
    @sulthan6131 Před rokem

    Love u lots❤

  • @hemantjagtap7340
    @hemantjagtap7340 Před rokem +1

    Striver, when will you upload the remaining vedios of Binary Search playlist ?

  • @kushagramishra3026
    @kushagramishra3026 Před rokem

    Understood.

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

    Understood

  • @anshujaiswal5622
    @anshujaiswal5622 Před 3 měsíci

    Understood :)

  • @dreamyme543
    @dreamyme543 Před 9 měsíci

    Understood🙃

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

    understood, for java people who are getting stuck in 25th test case, instead of using == operator, use .equals and it will pass.

  • @seemameghasai6744
    @seemameghasai6744 Před 11 měsíci +1

    @takeUforward ,Since first two and last indices are same you can do low = 2 and high = arr.length-3 right?

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

    MIND BLOWN AT @7:14 DAyummmmmmmm!!!!!!!

  • @user-js1rx8rs9p
    @user-js1rx8rs9p Před 3 měsíci

    understood bro
    😎😎😎

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

    What if a no repeated odd no of times so In this case this technique of dealing with indexes will me valid or not?
    For example
    {1,1,1,2,3,3}
    Here find the single element

  • @NavyaVedachala
    @NavyaVedachala Před rokem +1

    Is it possible to access the previous version of the A2Z DSA sheet? Links to problems and some videos are missing. There are also five problems missing. Is it possible to access these problems on a Google sheet if you have one until the glitch is fixed? Thank you! Loving the series

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

    class Solution {
    public:
    int singleNonDuplicate(vector& nums) {
    int ans=0;
    for(int i=0;i

  • @soumiyamuthuraj3516
    @soumiyamuthuraj3516 Před 28 dny

    awesome

  • @girishbhargava6367
    @girishbhargava6367 Před rokem

    Will binary search on answer be covered in this series?

  • @badrirao6472
    @badrirao6472 Před 2 měsíci +1

    How can we do it through binary search if there are two single elements in an array ?

  • @be-sreenu2202
    @be-sreenu2202 Před měsícem +1

    it might be possible using XOR but TC-O(N)

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

    thanks sir

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

    Thanks Lord Striver

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

    understood😁

  • @VINAYSINGH-wc8sq
    @VINAYSINGH-wc8sq Před rokem

    🔥🔥🔥

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

    vere level

  • @user-kx6fi8ou9f
    @user-kx6fi8ou9f Před měsícem

    bhaiya bawal ho aap

  • @suryasaimaheswar8636
    @suryasaimaheswar8636 Před 25 dny

    understood:)

  • @mohdbilal2
    @mohdbilal2 Před rokem +1

    simple brute force is take xor of all element with xorr=0;
    ultimately you will get single element

    • @takeUforward
      @takeUforward  Před rokem +2

      Yes but the reason of saying this brute force was to explain the thought process of binary search

    • @mohdbilal2
      @mohdbilal2 Před rokem

      @@takeUforward Okay, Thanks brother 🥰 You are doing a great work.