BS-8. Single Element in Sorted Array

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

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

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

    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 ปีที่แล้ว +4

      Outstanding 😊

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

      Understood

    • @DeepakKumar-uq9js
      @DeepakKumar-uq9js ปีที่แล้ว +1

      Thanks bhai Ji😃

    • @samuelfrank1369
      @samuelfrank1369 11 หลายเดือนก่อน +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 7 หลายเดือนก่อน +3

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

  • @yashmundada2483
    @yashmundada2483 3 หลายเดือนก่อน +20

    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!

    • @thefourhourtalk
      @thefourhourtalk 2 หลายเดือนก่อน

      thanks buddy

    • @nehalchaudhary3319
      @nehalchaudhary3319 14 วันที่ผ่านมา

      These made this ques just a piece of cake, thanks 👍🏻

  • @ratnadeepsaha7675
    @ratnadeepsaha7675 ปีที่แล้ว +65

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

  • @Harshitha45-sde
    @Harshitha45-sde หลายเดือนก่อน +3

    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!

  • @arijitroy8652
    @arijitroy8652 7 หลายเดือนก่อน +3

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

  • @AdityaKumar-be7hx
    @AdityaKumar-be7hx ปีที่แล้ว +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.

  • @harshsolanki1058
    @harshsolanki1058 11 หลายเดือนก่อน +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 9 หลายเดือนก่อน +2

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

    • @shibainu7500
      @shibainu7500 5 หลายเดือนก่อน

      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)

  • @lakshyagupta9435
    @lakshyagupta9435 10 หลายเดือนก่อน +6

    The way you explained the approach is just awesome.

  • @user-sd1ou1db1p
    @user-sd1ou1db1p ปีที่แล้ว +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.

  • @adityanigam8373
    @adityanigam8373 ปีที่แล้ว +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

  • @chiragsharma8905
    @chiragsharma8905 10 หลายเดือนก่อน +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.

  • @YogeshKumar-px5bd
    @YogeshKumar-px5bd ปีที่แล้ว +4

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

  • @raghavmanish24
    @raghavmanish24 7 หลายเดือนก่อน +2

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

  • @ujjawal_
    @ujjawal_ ปีที่แล้ว +14

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

  • @abid3801
    @abid3801 6 หลายเดือนก่อน +7

    Striver one dumb question. When you started practicing DSA .Did you able to come up these tricks on your own at least for some?

  • @baderajesh23
    @baderajesh23 3 หลายเดือนก่อน +1

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

  • @abhirajrohilla
    @abhirajrohilla 7 หลายเดือนก่อน +1

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

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

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

    • @sayanpradhan1366
      @sayanpradhan1366 2 หลายเดือนก่อน +2

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

  • @MJBZG
    @MJBZG 2 หลายเดือนก่อน +1

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

  • @senseiAree
    @senseiAree 11 หลายเดือนก่อน +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.

  • @TrendyGamer007
    @TrendyGamer007 ปีที่แล้ว +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 👍🏻👍🏻👍🏻.

  • @chethanprabhu4475
    @chethanprabhu4475 2 หลายเดือนก่อน

    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.

  • @PankajSingh-kz7or
    @PankajSingh-kz7or หลายเดือนก่อน

    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

  • @gauravmasand
    @gauravmasand 17 วันที่ผ่านมา

    Hey Striver this is one better code like i use mod operator to change the value of mid and i find ur approch is bit of lengthy and this has also O(log n) time and O(1) space complexity
    class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
    if (len(nums)==1): return nums[0]
    left, right = 0, len(nums) - 1
    while left < right:
    mid = (left + right) // 2
    # Ensure mid is even so that it pairs with mid + 1
    if mid % 2 == 1:
    mid -= 1
    # If the pair is correct, the single element is to the right
    if nums[mid] == nums[mid + 1]:
    left = mid + 2
    else:
    right = mid
    # left will be pointing to the single element
    return nums[left]

  • @AbhishekSingh-cq2jx
    @AbhishekSingh-cq2jx 11 หลายเดือนก่อน

    understood better than Scaler paid cource really Thank You

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

    Understood , amazing explanation as always.

  • @abhishekkumar-fe8lw
    @abhishekkumar-fe8lw 11 หลายเดือนก่อน

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

  • @Manasidas99
    @Manasidas99 11 หลายเดือนก่อน +1

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

  • @ashishranjan8350
    @ashishranjan8350 ปีที่แล้ว +4

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

  • @iamprateek3220
    @iamprateek3220 4 หลายเดือนก่อน

    Another solution:
    int singleNonDuplicate(vector& arr)
    {
    int n= arr.size();
    int l=0, r=n-1;
    if(n==1){
    return arr[0];
    }
    while(l

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

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

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

    You are the king 👑 of coding striver

  • @tech1hutz
    @tech1hutz ปีที่แล้ว +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 ...

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

      yes bhaiya, plz make one on greedy

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

    Understood Sir, thanks a lot for this amazing video.

  • @shahbazaalam7559
    @shahbazaalam7559 7 หลายเดือนก่อน

    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

  • @Vamshi9876
    @Vamshi9876 6 หลายเดือนก่อน

    Thanks. Great way of explaining complex questions.

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

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

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

    Mind blowing explanation.

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

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

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

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

  • @VasanthChoudary-uc5cz
    @VasanthChoudary-uc5cz 9 หลายเดือนก่อน

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

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

    When to trim the right half and left half is not clear properly plz emphasise on that part only plzzzz and rest is just amazing love form our side

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

      Here it was easy, so did not focus much, if you take a pen and paper, it is an easy problem! With problems we have, stay rest assured.

  • @ashwath1914
    @ashwath1914 ปีที่แล้ว +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

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

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

  • @raghavsm9976
    @raghavsm9976 3 หลายเดือนก่อน +1

    Understood

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

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

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

    Quite interesting question!

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

    Thank you Striver...Understood everything

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

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

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

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

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

    UNDERSTOOD

  • @be-sreenu2202
    @be-sreenu2202 3 หลายเดือนก่อน +1

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

  • @udatyadeb101
    @udatyadeb101 7 หลายเดือนก่อน

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

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

    Understood ❤

  • @rkgk5445
    @rkgk5445 25 วันที่ผ่านมา

    The way you say Single 😀, I don't think it's your problem Sir... right now
    Although, kudos to your way of explanation...👏

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

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

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

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

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

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

  • @VenusaiYalamanchili
    @VenusaiYalamanchili 4 หลายเดือนก่อน +1

    How am I gonna get that observation of even odd, or odd ,even is it easy or am I the only one who felt amazed when Striver said that?

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

      count me also

    • @sarahshakil14
      @sarahshakil14 10 วันที่ผ่านมา

      You're not alone.
      I feel so dumb, I can't tell you! 😭

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

    Such an incredible work!!

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

    To further optimise this BS approach, we can add a condition of (!(mid & 1) && nums[mid] != nums[mid - 1] && nums[mid] != nums[mid + 1]) as the single element will always be at an even index.

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

    excellenttttt explaination..!!!!! Understood

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

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

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

    Good explanation with dry run understood

  • @NazeerBashaShaik
    @NazeerBashaShaik 5 หลายเดือนก่อน

    Understood, thank you.

  • @kingbadshah452
    @kingbadshah452 6 หลายเดือนก่อน

    thanks striver understood everything

  • @AshmitIITH
    @AshmitIITH 2 หลายเดือนก่อน

    This problem can also be done using a hashmap or dictionary but with O(n) tc. Still it can be better approach that brute force approach and we can tell this method in the interview ig

  • @pokigamerop
    @pokigamerop 11 หลายเดือนก่อน +1

    My O(1) solution:
    That Single element is me :)

  • @samuelfrank1369
    @samuelfrank1369 11 หลายเดือนก่อน

    Understood. Thanks a lot.

  • @sagarshah5341
    @sagarshah5341 7 หลายเดือนก่อน

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

  • @meetdedhia8025
    @meetdedhia8025 7 หลายเดือนก่อน +1

    what if there was 3 instead of 4 in the last example? If both at sides there were 3 then how would the search have operated?

    • @neetashinde7362
      @neetashinde7362 6 หลายเดือนก่อน

      I was searching for the same

  • @thenriquevicentini
    @thenriquevicentini 4 หลายเดือนก่อน

    Understood!

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

    Understood, but your code can be made short as
    int singleNonDuplicate(vector& a)
    {
    int n=a.size();
    int l=0,r=n-1;
    while(l

  • @alheraahmad5481
    @alheraahmad5481 2 วันที่ผ่านมา

    understood!

  • @badrirao6472
    @badrirao6472 3 หลายเดือนก่อน +1

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

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

    Understood.

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

    Amazing explanation ❣❣

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

    superb explanation

  • @khalasianiket816
    @khalasianiket816 2 หลายเดือนก่อน

    understood ❤

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

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

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

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

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

    Understood✅🔥🔥

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

    Plz upload rest of the video as soon as possible

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

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

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

    Shandaar.

  • @NavyaVedachala
    @NavyaVedachala ปีที่แล้ว +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

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

    Understood ❤🎉

  • @rahulkumarbarik7584
    @rahulkumarbarik7584 11 หลายเดือนก่อน +2

    That single element is me 🥲

  • @SibiRanganathL
    @SibiRanganathL 6 หลายเดือนก่อน

    Understood but need to revise.

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

    This was a good question to understand BS But if we have time constraints we should go for the liner XOR operation of each element in the array and store the xor. XOR will ultimately eliminate the 2 times appearing numbers and we will end up getting the single occurring number in the array.
    Great solution by Striver as alway ❤❤

    • @montypannu1975
      @montypannu1975 ปีที่แล้ว +4

      Yes but it will have T.C of O(N) and with binary search we get O(logN)

  • @harshilpatel3205
    @harshilpatel3205 7 หลายเดือนก่อน

    Understood sir 😇😊

  • @anshujaiswal5622
    @anshujaiswal5622 4 หลายเดือนก่อน

    Understood :)

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 6 หลายเดือนก่อน

    Thank you Bhaiya

  • @abhay9994
    @abhay9994 10 หลายเดือนก่อน

    Thank You.

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

    Kya mast thumbnail hai.. 🔥

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

    understood bhaiya

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

    Understood @striver ❤🙌🥳

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

    What happens if there are not exactly 2 elements which are duplicates? What if there are more than 2, in this case how can we identify the sequence?

  • @Harshraj-xw4wb
    @Harshraj-xw4wb ปีที่แล้ว

    Understood

  • @amitgundoba9575
    @amitgundoba9575 18 วันที่ผ่านมา

    similar simple code in c++
    class Solution {
    public:
    int singleNonDuplicate(vector& nums) {
    int s = 0;
    int e = nums.size() - 2;
    while (s

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

    Understoooood

  • @CoderGrow1
    @CoderGrow1 7 หลายเดือนก่อน

    thanks sir