Single element in a sorted array | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • This video explains a very important programming interview question which is to find the unique element in a sorted array in just O(logN) time and O(1) extra space. This problem would have been extremely easy to solve provided we were allowed O(N) time. This can be solved by simple linear search or XOR operation. In order to take benefit of sorted array property, we can use binary search algorithm with some observations to find the unique element in just O(logN). I have shown 4 observations and used them to solve the problem in O(logN) time using binary search algorithm.At the end of the video, i have explained the CODE. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: gist.github.co...

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

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

    Easy approach to toughest of the questions...This man is simply a legend !! 🙌🙌

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

    Shocked to see that you have around 90k subscribers . You deserve atleast 500k subscribers dude !

  • @TechBroQuiz
    @TechBroQuiz 3 ปีที่แล้ว +6

    Finally understood this question after seeing four different videos.Thanks a lot🙏

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

      oh yeah... this is easily the most intuitive of all the other videos for this question.

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

    You are the best channel for these solution videos! Your explanations are so clear and are very helpful for me during the job search!!!

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

    class Solution {
    public:
    int singleNonDuplicate(vector& nums) {
    int n = nums[0];
    for(int i=1;i

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

      Thanks for sharing

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

    Amazing, i saw a lots of video but i did not clear the concept but you are amazing and you know how to teachs

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

    very nice and different solution thanks alot I was very confused how will we change the left and right values but this video helped me clear my doubts.

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

    Great Explanation better than anyone on TH-cam!!

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

    can you make some videos on hard graph,DP and trees problems.
    Please make some videos on binary lifting, euler tour , etc and their questions. I have just listened their names and don't have idea that what are they, and its confusing to studying codes, your videos are helpful for that purpose. Please make some videos on these topics and cover some hard questions which are asked in interviews

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

      Yea I am on my way. Problem is, I don't have my GPU now and so rendering is very difficult and time taking. I am still trying to cover all topics of graph.

    • @ArpitDhamija
      @ArpitDhamija 4 ปีที่แล้ว

      @@techdose4u thanx

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

    TechDose: I have one query , after seen the logic Its very easy to understand the approach .But could you please help me how should I build my own logic or approach to solve the any problems of programming. Thanks

    • @ahmedjubayer5419
      @ahmedjubayer5419 2 ปีที่แล้ว

      practice and practice. there is not alternative.

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

    Wonderful explanation ,and I am thankful for your efforts which help me to understand the concept.

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

    Thanks a lot Surya! Couldn't think of this approach myself.

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Thanks :) You are not replying now 🥺

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

    great Explanation!!! thanks, but do we need to check that odd even index to decide which partition to check, what if we do normal BS, like if x>mid then low=mid+1 elif x

    • @agammishra9674
      @agammishra9674 4 ปีที่แล้ว

      Please do reply, I think I am missing something

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      This will not work. To decide which subarray to search, you need to check index.

    • @mukulupadhyay4656
      @mukulupadhyay4656 2 ปีที่แล้ว

      how would you find that X you are searching for that only, you are not given the target in this question unlike normal binary search.

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

    u explained it so well........ u r simply a legend !! 🙌🙌

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

    thanks a lot, very easy to grasp ur explanation

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

    Can u please increase the size of code in your upcoming videos. Appreciate , what u are doing.

  • @captain_vegan
    @captain_vegan 2 ปีที่แล้ว

    Very helpfull thank you, u deserve million subs, man

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

    Best explanation for this problem

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

    Bhetreen samjhaya Bhai 👏🏼🙆🏻‍♂️

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Dhanyawaad bhai :)

  • @vivek.tiwary
    @vivek.tiwary 2 ปีที่แล้ว +1

    As usual, great explanation !!. Do you have any playlist for binary search?

  • @Harish-ll9tv
    @Harish-ll9tv ปีที่แล้ว

    Sir ,i done this problem like this int singleNonDuplicate(vector& nums) {
    int ans=0;
    for(int i=0;i

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

    NIce intuitive solution.

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

    you are superb man!! keep making videos . Your channel helps a lot

  • @rahulbhalla9891
    @rahulbhalla9891 2 ปีที่แล้ว

    Please upload more and more videos. Your video is very helpful

  • @letsdoeverythinginoneweek9398
    @letsdoeverythinginoneweek9398 3 ปีที่แล้ว

    nice bro
    why i like u because
    my approach && my thinking ==your approach + your explanation

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

    Why we don't use Counting Algorithm ?? If the Count==1 return True? Is this Take More Time I guess O(N+K)? BST gave us Optimal Solution Log(N) ?? Is it?

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

      Nope. It will take O(N) because you need to visit all elements to count them.

  • @cristianouzumaki2455
    @cristianouzumaki2455 2 ปีที่แล้ว

    Your explanations are gems. Thanks sir.

  • @MJ-zs5jv
    @MJ-zs5jv ปีที่แล้ว

    Such a great explanation!

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

    Sir what if the question was that the elements can occur more than 2 times except for the unique element then will this approach work ??...or what will be the right way to solve that question other than using O(N) time

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

      Array should have special property to capitalise on. Otherwise, there won't be a way less than O(N).

    • @madhavsahi7400
      @madhavsahi7400 4 ปีที่แล้ว

      @@techdose4u okay sir thanks

  • @NikhilKumar-oy7mx
    @NikhilKumar-oy7mx 4 ปีที่แล้ว +1

    Have you seen the best submission? It is the xor one taking 0ms but how as it's O(n) while binary will take log n.
    Please reply.

    • @NikhilKumar-oy7mx
      @NikhilKumar-oy7mx 4 ปีที่แล้ว

      I was waiting for your video to ask question 😉

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

      It was because of low and weak tests cases. Also, he optimized every possible stream for max IO. He wrote it separately. You can see it.

    • @NikhilKumar-oy7mx
      @NikhilKumar-oy7mx 4 ปีที่แล้ว +1

      @@techdose4u how does max IO work

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      By Max io I meant max io speed.

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

      Don't look at those stupid stats. They mean nothing. If you submit multiple times you will see you get different times. Just make sure you have the best time complexity and you are good to go.

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

    how you came up with this partition property and pair index property? (how you got that intuition). Please explain

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

      By seeing the question I knew we had to use binary search. Now the question was how to use it. We could have used it using array values. So the 2nd obvious thing was index and yes it worked. That's how.

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

      @@techdose4u stay the same man! you actually need not reply but every time I ask you, I am getting a reply! Thank You. Please post more about the intuition and various concepts involved in computer science to land a Software Engineer job

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

      Yea sure.

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

    Sir i am in a confusion of using low=mid+1, high = mid ; and taking while(low

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

      That's a good question. Actually it depends on your formula being used. The best formula for mid is low+(high-low)/2. For this, we generally use low

    • @sunnyjain2444
      @sunnyjain2444 4 ปีที่แล้ว

      @@techdose4u What I think is , if you use while(low

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

    Thats awesome man! Keep up the good work!

  • @uv9111
    @uv9111 2 ปีที่แล้ว

    Amazingly explained

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

    great solution

  • @gokulnaathbaskar9808
    @gokulnaathbaskar9808 2 ปีที่แล้ว

    Thank you so much!
    Loved the pair index property

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

    return sum(set(nums))*2-sum(nums)

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

    Simplest Approach :)

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

    No words, really Hats off to you brother... :D

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Thanks :)

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

      @@techdose4u Hey brother, could you please help me out, I am not able to understand the "Median of two sorted arrays of different sizes", please help me out with this question, youtube doesn't have a good explanation of this question.

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

      I will soon make a video on that :)

    • @marvellouschandan
      @marvellouschandan 3 ปีที่แล้ว

      @@techdose4u Thank you :D

  • @mohammedyunusshaikh5080
    @mohammedyunusshaikh5080 2 ปีที่แล้ว

    Thankyou Sir for the great explanation

  • @ambarishrishu
    @ambarishrishu 2 ปีที่แล้ว

    nice video keep it up. do the solution in python. most people like me uses the python.

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

    Awesome dada

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

    Good approach 👏👏

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

    Can you tell about what is ios-basesync and also after this line cin.tie?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      This is for fast io in c++ by optimizing io streams. Read about it on internet.

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

    alternate code:
    int singleNonDuplicate(vector& nums) {
    int low=0,high=nums.size()-1;
    if(high==0){
    return nums[0];
    }
    while(low

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

      where we don't need to check for the adjacent element which may give the array index out of bound error. C++ leetcode all testcase passed.

  • @newBieGuy05
    @newBieGuy05 2 ปีที่แล้ว

    awesome explanation bro

  • @GhostRider....
    @GhostRider.... 2 ปีที่แล้ว

    Nice explanation sir 🔥🔥

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

    I used map and then check occurrence if it is one i return it
    Is it right approach
    And time complexity?

    • @vishnuvardhan-wq5qi
      @vishnuvardhan-wq5qi 4 ปีที่แล้ว +1

      I think its O(n)

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      It is not a good technique. Question asked to solve in LogN due to special property of array.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes you are right.

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

      It is O(n) time and o(n) space. The question says to do in logn time and constant space

    • @GauravKumar-wm4cm
      @GauravKumar-wm4cm 4 ปีที่แล้ว +1

      @@techdose4u we could have used xor or array ans we will get the ans 2 line code

  • @TarunSharma-iv7iv
    @TarunSharma-iv7iv 4 ปีที่แล้ว +1

    Great Explanation!!

  • @guptasaurabh688
    @guptasaurabh688 4 ปีที่แล้ว

    Which tool you are using with this pen to explain? Pls reply

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

    Love your explanation ❤

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

    I'm waiting Hurrah!

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

    The best explanation, Thank you so much for making great content.

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

    This will works when duplicate elements exits in pairs

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

    Hey, can you please tell me which problems to solve in graph and dp, you have earlier said 10 problems of dp in our video. But still can you please tell me so that I can cover all the patterns of dp. The graph and dp concept is slightly hard.

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

      Try to cover all top 20 questions from all ropics on geeksforgeeks. This should get you going.

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

      @@techdose4u Okay thank you

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Welcome :)

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

    Thanks

  • @AjaySingh-ll5qw
    @AjaySingh-ll5qw 4 ปีที่แล้ว +1

    Nice..

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

    Awesome as always!!

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

    Nice explanation!

  • @v.sreesairam9488
    @v.sreesairam9488 3 ปีที่แล้ว +1

    bhaiya is it ok to do mid=(low+high)/2 inside the binary search

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

      It depends. Just do a dry run and check.

    • @v.sreesairam9488
      @v.sreesairam9488 3 ปีที่แล้ว

      Ok bhaiya thanks for your reply

    • @krishnakantahirwar6418
      @krishnakantahirwar6418 3 ปีที่แล้ว

      It sometime gives you stack overflow otherwise it's okk👍👍

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

    Thank you

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

    Thank you so much.

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

    amazing sir.

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

    if (nums[mid] == nums[mid ^ 1]) can handle even and odd

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes you can use this or bitwise & as well.

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

    public int singleNonDuplicate(int[] nums) {
    int l = 0, h = nums.length - 1, mid;
    while(l < h) {
    mid = l + (h - l)/2;
    if(mid%2 == 1) mid--;
    if(nums[mid] == nums[mid+1]) l = mid + 2;
    else h = mid;
    }
    return nums[l];
    }
    how about this one

  • @nagajaswanth831
    @nagajaswanth831 4 ปีที่แล้ว

    did it in python:
    class Solution(object):
    def singleNonDuplicate(self, nums):
    n=len(nums)
    low=0
    high=n-1
    while low=1 and mid

  • @_ankush_verma
    @_ankush_verma 2 ปีที่แล้ว

    I doubt that it won't work in case when some elements are triplets then pair property will not help[

  • @PersistentProgrammer
    @PersistentProgrammer 4 ปีที่แล้ว

    Python Version:
    class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
    """
    :type nums: List[int]
    :rtype: int
    """
    left = 0
    right = len(nums) - 1
    #Edge case
    if right == 0 or nums[0] != nums[1]:
    return nums[0]
    if nums[right] != nums[right-1]:
    return nums[right]
    while left

  • @yaswanthp2294
    @yaswanthp2294 2 ปีที่แล้ว

    Is it allowed to have same element more than two

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

    bro you are awesome :)

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

    Will it matter that array is sorted or not ? I don't think so.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      The pair property won't work. This technique is very specific.

    • @sonuagarwal7679
      @sonuagarwal7679 4 ปีที่แล้ว

      So basically sorting is bringing same number together.
      Thanks

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

    Amazing

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

    Thankuuuuu

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

    So how pair index proper holds in this case. Start at even index and end at odd index
    [1,1,1,2,3,3]?

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

      Every element appears exactly twice, except for one element which appears exactly once

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Your input is wrong. It doesn't comply with question.

    • @vishnuvardhan-wq5qi
      @vishnuvardhan-wq5qi 4 ปีที่แล้ว

      @@techdose4u so the input must be like all the other numbers except the unique number should follow same pattern .. am i correct??

    • @niranjanhegde1535
      @niranjanhegde1535 4 ปีที่แล้ว

      @@ianpan0102 ohh. Correct. Thanks

  • @nick-sx2zn
    @nick-sx2zn 2 ปีที่แล้ว

    what if there are theree 1s on left side

  • @yaswanthp2294
    @yaswanthp2294 2 ปีที่แล้ว

    It only works when each element exactly appears twice, but one element appears exactly once
    Please clarify that

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

    Hey anyone what is purpose of checking the boundary element

  • @oladimejijames9554
    @oladimejijames9554 4 ปีที่แล้ว

    Hello sir, i did it like this and passed all the test cases, can you help improve this solution:
    if(numbers.length==1)
    return numbers[0];
    int current = 0;
    final int nonRepeated = 0;
    Map mp = new HashMap();
    for (int i = 0; i

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

    what if there are 3(three 1 1 1 2 2 3 4 4 ) 1's at the starting

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      There cannot be....because this doesn't follow the question constraints. Please read the question carefully.

  • @navinchainani4721
    @navinchainani4721 3 ปีที่แล้ว

    We can use xor its too easy

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

    class Solution {
    public int singleNonDuplicate(int[] nums) {
    int low = 0;
    int high = nums.length - 1;
    if(high==0)
    return nums[0];
    else if(nums[0]!=nums[1])
    return nums[0];
    else if(nums[high]!=nums[high-1])
    return nums[high];
    while(low

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

    U dint explian code

  • @kushgupta1187
    @kushgupta1187 4 ปีที่แล้ว

    public int singleNonDuplicate(int[] nums) {
    int low=0;
    int high=nums.length-1;
    int mid=0;
    while(low

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

    int singleNonDuplicate(vector& nums) {
    int lo = 0, hi = nums.size() - 1;
    if (hi == 0)
    return nums[0];
    else if (nums[0] != nums[1])
    return nums[0];
    else if (nums[hi] != nums[hi - 1])
    return nums[hi];
    while (lo

  • @sayanmaitra11
    @sayanmaitra11 2 ปีที่แล้ว

    thanks