BS-8. Single Element in Sorted Array

Поделиться
HTML-код
  • Опубликовано: 7 фев 2025

Комментарии • 386

  • @takeUforward
    @takeUforward  Год назад +207

    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 Год назад +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 Год назад +5

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

  • @ratnadeepsaha7675
    @ratnadeepsaha7675 Год назад +86

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

  • @yashmundada2483
    @yashmundada2483 8 месяцев назад +52

    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!

  • @sanjaybhorkar8068
    @sanjaybhorkar8068 Месяц назад +8

    SOLVED THIS PROBLEM ON MY OWN , ANOTHER LEVEL OF SATISFACTION!!!

  • @lakshyagupta9435
    @lakshyagupta9435 Год назад +9

    The way you explained the approach is just awesome.

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

  • @PrakharSrivastava-k2d
    @PrakharSrivastava-k2d Год назад +6

    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.

  • @Harshitha_sharma_27
    @Harshitha_sharma_27 6 месяцев назад +6

    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!

  • @shivajirao999
    @shivajirao999 8 месяцев назад +4

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

    • @sayanpradhan1366
      @sayanpradhan1366 7 месяцев назад +5

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

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

  • @harshsolanki1058
    @harshsolanki1058 Год назад +7

    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 Год назад +3

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

    • @shibainu7500
      @shibainu7500 10 месяцев назад +2

      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)

  • @raghavmanish24
    @raghavmanish24 Год назад +2

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

  • @YogeshKumar-px5bd
    @YogeshKumar-px5bd Год назад +4

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

  • @neerajkumar-ts6om
    @neerajkumar-ts6om 4 месяца назад +7

    Ok so now onwards I will try to solve the edge cases before in the problem, this will allow me to focus on the main logical part and reduce stress.

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

  • @chiragsharma8905
    @chiragsharma8905 Год назад +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.

  • @AbhishekSingh-cq2jx
    @AbhishekSingh-cq2jx Год назад +1

    understood better than Scaler paid cource really Thank You

  • @baderajesh23
    @baderajesh23 9 месяцев назад +1

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

  • @Satya-g5t
    @Satya-g5t 3 месяца назад

    I really appreciate your enthusiasm and solutions.

  • @toxaim9936
    @toxaim9936 4 месяца назад +1

    i did it by length till mid approach(different way to look at same thing):
    1) if length from low to mid is odd then storing only pairs will overflow the last pair such that num[m]==num[m+1] , so move right
    2) if len from low to mid is even then storing only pairs will exhaust the length such that
    num[m-1]==num[m], so we move right

  • @Manasidas99
    @Manasidas99 Год назад +1

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

  • @charan123rams3
    @charan123rams3 Год назад +1

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

  • @Shunya_Advait
    @Shunya_Advait Год назад +1

    Understood Sir, thanks a lot for this amazing video.

  • @bapanapallihemanth705
    @bapanapallihemanth705 Год назад

    You are the king 👑 of coding striver

  • @abhirajrohilla
    @abhirajrohilla Год назад +1

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

  • @cinime
    @cinime Год назад

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

  • @arijitroy8652
    @arijitroy8652 Год назад +3

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

    • @aamir4684
      @aamir4684 Месяц назад

      bhai lekin time complexicity zayada aa jaye gi

  • @bhupendramaurya6587
    @bhupendramaurya6587 Год назад

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

  • @SAGAR-wk4wv
    @SAGAR-wk4wv 15 часов назад +1

    I used xor method as a brute approach.

  • @TechHutz
    @TechHutz Год назад +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 ...

  • @sushantguria8820
    @sushantguria8820 6 месяцев назад

    Understood , amazing explanation as always.

  • @Vamshi9876
    @Vamshi9876 11 месяцев назад

    Thanks. Great way of explaining complex questions.

  • @vaibhavchougule-vs6fs
    @vaibhavchougule-vs6fs 18 дней назад

    Thanks sir For This Amazing Explanation ❤❤❤❤

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

  • @senseiAree
    @senseiAree Год назад +3

    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.

  • @vijaynag7723
    @vijaynag7723 Год назад

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

  • @djsanket25
    @djsanket25 4 месяца назад

    sir please create a course on English speaking , i loved your way of speaking . you course will be brilliant one. it will be very helpfull❤❤❤❤

  • @sayakghosh5104
    @sayakghosh5104 Год назад +1

    Mind blowing explanation.

  • @onetapgaming123-v2x
    @onetapgaming123-v2x 16 дней назад +2

    23/01/25🎉8th video 🌄 straight 👍

  • @PankajSingh-kz7or
    @PankajSingh-kz7or 6 месяцев назад

    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

  • @yashraj5898
    @yashraj5898 3 месяца назад

    understood ! great work buddy !

  • @ashishranjan8350
    @ashishranjan8350 Год назад +4

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

  • @hiteshpanchal5772
    @hiteshpanchal5772 Год назад

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

  • @manavsingh5919
    @manavsingh5919 Год назад

    Thank you Striver...Understood everything

  • @chethanprabhu4475
    @chethanprabhu4475 7 месяцев назад

    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.

  • @Pooja-qv7hr
    @Pooja-qv7hr 26 дней назад

    amazing explanation thanks a lot

  • @MJBZG
    @MJBZG 7 месяцев назад +2

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

  • @prathmeshparab7046
    @prathmeshparab7046 Год назад +1

    Amazing explanation ❣❣

  • @gauravmasand
    @gauravmasand 5 месяцев назад

    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]

  • @abid3801
    @abid3801 Год назад +18

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

  • @58harshverma57
    @58harshverma57 Год назад +1

    Quite interesting question!

  • @maaz.i7
    @maaz.i7 4 месяца назад

    Can be done by XOR
    Just keep doing XOR from start to end of array, the element which appears only once will be the final result of XOR, rest will become 0 because A XOR A = 0 and 0 XOR A = A

  • @jeehub041
    @jeehub041 Год назад +1

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

  • @abhishekkumar-fe8lw
    @abhishekkumar-fe8lw Год назад

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

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

  • @chphanisimha7802
    @chphanisimha7802 Год назад

    Such an incredible work!!

  • @udatyadeb101
    @udatyadeb101 Год назад

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

  • @iamprateek3220
    @iamprateek3220 9 месяцев назад

    Another solution:
    int singleNonDuplicate(vector& arr)
    {
    int n= arr.size();
    int l=0, r=n-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.

  • @sufyainposharkar2080
    @sufyainposharkar2080 2 месяца назад

    Here's the code with same logic but simpler,
    class Solution {
    public:
    int singleNonDuplicate(vector& nums)
    {
    int low = 0;
    int high = nums.size() - 1;
    while (low < high) {
    int mid = low + (high - low) / 2;
    // Ensure mid is even for comparison with mid + 1
    if (mid % 2 == 1) mid--;
    // If pair is found, the unique element is in the right half
    if (nums[mid] == nums[mid + 1]) {
    low = mid + 2;
    } else {
    // Otherwise, it's in the left half
    high = mid;
    }
    }
    return nums[low];
    }
    };

  • @rushidesai2836
    @rushidesai2836 4 месяца назад

    Weirdly good question.

  • @bhavyasrikanchi5753
    @bhavyasrikanchi5753 Год назад

    excellenttttt explaination..!!!!! Understood

  • @kingbadshah452
    @kingbadshah452 Год назад

    thanks striver understood everything

  • @arpitamanderna9111
    @arpitamanderna9111 Год назад +1

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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Год назад

    Good explanation with dry run understood

  • @ayushgaurabh8604
    @ayushgaurabh8604 Год назад

    superb explanation

  • @VenusaiYalamanchili
    @VenusaiYalamanchili 9 месяцев назад +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 9 месяцев назад

      count me also

    • @hidden_star14
      @hidden_star14 5 месяцев назад

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

    • @ShortsChachu
      @ShortsChachu 4 месяца назад

      ​@@hidden_star14Same bro 😢

  • @rkgk5445
    @rkgk5445 6 месяцев назад

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

  • @VasanthChoudary-uc5cz
    @VasanthChoudary-uc5cz Год назад

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

  • @atharva_g_vlogs
    @atharva_g_vlogs 4 месяца назад

    UNDERSTOOD SIR

  • @mohitsingh13
    @mohitsingh13 6 месяцев назад

    Understood ❤

  • @samuelfrank1369
    @samuelfrank1369 Год назад

    Understood. Thanks a lot.

  • @AYJ959
    @AYJ959 Год назад

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

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

  • @NazeerBashaShaik
    @NazeerBashaShaik 10 месяцев назад

    Understood, thank you.

  • @sandeep4915
    @sandeep4915 4 месяца назад

    that trimming❤❤

  • @Dhanasri_Parisam
    @Dhanasri_Parisam 2 месяца назад

    just awesome.

  • @ErenYeager-dp4er
    @ErenYeager-dp4er 27 дней назад

    Done on 12 Jan 2025 at 13:57
    Place : Study Room 2 , Hostel 5 , IIT Bombay

  • @adarshkumarrao3478
    @adarshkumarrao3478 Год назад +1

    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

  • @sagarshah5341
    @sagarshah5341 Год назад

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

  • @anonymous-sz8jq
    @anonymous-sz8jq 12 дней назад

    understoood!!

  • @aakashsharma780
    @aakashsharma780 Год назад

    Understood @striver ❤🙌🥳

  • @guvanchh
    @guvanchh 17 дней назад

    Great!💥

  • @traymk
    @traymk Год назад

    Kya mast thumbnail hai.. 🔥

  • @pokigamerop
    @pokigamerop Год назад +1

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

  • @utsavseth6573
    @utsavseth6573 Год назад

    Shandaar.

  • @YourCodeVerse
    @YourCodeVerse Год назад

    Understood✅🔥🔥

  • @NikhilSharma2409_
    @NikhilSharma2409_ Месяц назад

    Understood😇😇

  • @divyadwivedi1527
    @divyadwivedi1527 Год назад

    Plz upload rest of the video as soon as possible

  • @mainakdasgupta7130
    @mainakdasgupta7130 4 дня назад

    thank you man

  • @yogeeshrsyogeeshrs4204
    @yogeeshrsyogeeshrs4204 Год назад

    Understood ❤🎉

  • @khalasianiket816
    @khalasianiket816 7 месяцев назад

    understood ❤

  • @DeepakPatel-d5v
    @DeepakPatel-d5v 9 месяцев назад

    Thanks a lot Bhaiya

  • @harshilpatel3205
    @harshilpatel3205 Год назад

    Understood sir 😇😊

  • @vigneshwaran_2002
    @vigneshwaran_2002 Год назад

    Thankyou sir very helpful❤

  • @hemantjagtap7340
    @hemantjagtap7340 Год назад +1

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

  • @bishalsarkar9205
    @bishalsarkar9205 8 месяцев назад

    Understood.

  • @seemameghasai6744
    @seemameghasai6744 Год назад +1

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

  • @badrirao6472
    @badrirao6472 8 месяцев назад +1

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