Single element in a sorted array | Leetcode

  Рет қаралды 53,892

Techdose

Techdose

4 жыл бұрын

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.com/SuryaPratapK/...

Пікірлер: 172
@NeverGiveUp186
@NeverGiveUp186 4 жыл бұрын
Easy approach to toughest of the questions...This man is simply a legend !! 🙌🙌
@techdose4u
@techdose4u 4 жыл бұрын
😅
@shreyasvishwakarma8979
@shreyasvishwakarma8979 2 жыл бұрын
Shocked to see that you have around 90k subscribers . You deserve atleast 500k subscribers dude !
@techdose4u
@techdose4u 2 жыл бұрын
😊 Thanks
@angrybutterflys
@angrybutterflys 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!!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@TechBroQuiz
@TechBroQuiz 3 жыл бұрын
Finally understood this question after seeing four different videos.Thanks a lot🙏
@ankushreddy9789
@ankushreddy9789 2 жыл бұрын
oh yeah... this is easily the most intuitive of all the other videos for this question.
@wasimahmad9927
@wasimahmad9927 7 ай бұрын
Amazing, i saw a lots of video but i did not clear the concept but you are amazing and you know how to teachs
@gokulnaathbaskar9808
@gokulnaathbaskar9808 Жыл бұрын
Thank you so much! Loved the pair index property
@cristianouzumaki2455
@cristianouzumaki2455 2 жыл бұрын
Your explanations are gems. Thanks sir.
@yitingg7942
@yitingg7942 3 жыл бұрын
Thanks a lot Surya! Couldn't think of this approach myself.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :) You are not replying now 🥺
@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.
@MJ-zs5jv
@MJ-zs5jv Жыл бұрын
Such a great explanation!
@uv9111
@uv9111 2 жыл бұрын
Amazingly explained
@nayankhuman1043
@nayankhuman1043 Жыл бұрын
Love your explanation ❤
@gautamtyagi8846
@gautamtyagi8846 Жыл бұрын
thanks a lot, very easy to grasp ur explanation
@rahulbhalla9891
@rahulbhalla9891 2 жыл бұрын
Please upload more and more videos. Your video is very helpful
@captain_vegan
@captain_vegan 2 жыл бұрын
Very helpfull thank you, u deserve million subs, man
@saraswatirathore3933
@saraswatirathore3933 2 жыл бұрын
Wonderful explanation ,and I am thankful for your efforts which help me to understand the concept.
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@vivek.tiwary
@vivek.tiwary 2 жыл бұрын
As usual, great explanation !!. Do you have any playlist for binary search?
@ashwinvarma9349
@ashwinvarma9349 3 жыл бұрын
Thats awesome man! Keep up the good work!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@ajaygonepuri2285
@ajaygonepuri2285 Жыл бұрын
Great Explanation better than anyone on KZbin!!
@techdose4u
@techdose4u Жыл бұрын
:)
@SirAkPandey
@SirAkPandey Жыл бұрын
Best explanation for this problem
@prathaps2753
@prathaps2753 4 жыл бұрын
you are superb man!! keep making videos . Your channel helps a lot
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@anurag_ad_01
@anurag_ad_01 Жыл бұрын
great solution
@adityajain3664
@adityajain3664 Жыл бұрын
NIce intuitive solution.
@mohammedyunusshaikh5080
@mohammedyunusshaikh5080 Жыл бұрын
Thankyou Sir for the great explanation
@GhostRider....
@GhostRider.... Жыл бұрын
Nice explanation sir 🔥🔥
@MohitKumar-zh9en
@MohitKumar-zh9en 2 жыл бұрын
awesome explanation bro
@agileprogramming7463
@agileprogramming7463 4 жыл бұрын
Awesome as always!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@oqant0424
@oqant0424 Жыл бұрын
u explained it so well........ u r simply a legend !! 🙌🙌
@techdose4u
@techdose4u Жыл бұрын
:)
@TarunSharma-iv7iv
@TarunSharma-iv7iv 4 жыл бұрын
Great Explanation!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@sourabhajoshi2129
@sourabhajoshi2129 6 ай бұрын
Thank you
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Nice explanation!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@shivatomar4319
@shivatomar4319 4 жыл бұрын
Bhetreen samjhaya Bhai 👏🏼🙆🏻‍♂️
@techdose4u
@techdose4u 4 жыл бұрын
Dhanyawaad bhai :)
@krishnavamsinadharikatla5150
@krishnavamsinadharikatla5150 4 жыл бұрын
Good approach 👏👏
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@sayanmaitra11
@sayanmaitra11 2 жыл бұрын
thanks
@letsdoeverythinginoneweek9398
@letsdoeverythinginoneweek9398 3 жыл бұрын
nice bro why i like u because my approach && my thinking ==your approach + your explanation
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@apoorvraizada
@apoorvraizada 3 жыл бұрын
The best explanation, Thank you so much for making great content.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@marvellouschandan
@marvellouschandan 3 жыл бұрын
No words, really Hats off to you brother... :D
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@marvellouschandan
@marvellouschandan 3 жыл бұрын
@@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 жыл бұрын
I will soon make a video on that :)
@marvellouschandan
@marvellouschandan 3 жыл бұрын
@@techdose4u Thank you :D
@priyanshukhullar5836
@priyanshukhullar5836 3 жыл бұрын
Awesome dada
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@shivarammuthukumaraswamy7164
@shivarammuthukumaraswamy7164 3 жыл бұрын
Thank you so much.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@ambarishrishu
@ambarishrishu Жыл бұрын
nice video keep it up. do the solution in python. most people like me uses the python.
@Harish-ll9tv
@Harish-ll9tv Жыл бұрын
Sir ,i done this problem like this int singleNonDuplicate(vector& nums) { int ans=0; for(int i=0;i
@ArpitDhamija
@ArpitDhamija 4 жыл бұрын
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 жыл бұрын
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
@agammishra9674
@agammishra9674 4 жыл бұрын
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.
@AjaySingh-ll5qw
@AjaySingh-ll5qw 4 жыл бұрын
Nice..
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@varunoyal87
@varunoyal87 3 жыл бұрын
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.
@sarthakchoudhary811
@sarthakchoudhary811 4 жыл бұрын
amazing sir.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@punjabicodingstyle5111
@punjabicodingstyle5111 4 жыл бұрын
Amazing
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@santoshbhupati3579
@santoshbhupati3579 2 жыл бұрын
Thanks
@techdose4u
@techdose4u 2 жыл бұрын
Welcome :)
@aniruddhabhattacharya807
@aniruddhabhattacharya807 4 жыл бұрын
class Solution { public: int singleNonDuplicate(vector& nums) { int n = nums[0]; for(int i=1;i
@techdose4u
@techdose4u 4 жыл бұрын
Thanks for sharing
@nikhillingam4630
@nikhillingam4630 4 жыл бұрын
I'm waiting Hurrah!
@techdose4u
@techdose4u 4 жыл бұрын
:)
@aniketbasu3865
@aniketbasu3865 2 жыл бұрын
bro you are awesome :)
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@punittripathi5461
@punittripathi5461 4 жыл бұрын
Can u please increase the size of code in your upcoming videos. Appreciate , what u are doing.
@techdose4u
@techdose4u 4 жыл бұрын
Okay sure
@madhavsahi7400
@madhavsahi7400 4 жыл бұрын
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 жыл бұрын
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
@dhanashreegodase4445
@dhanashreegodase4445 2 жыл бұрын
Thankuuuuu
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😀
@tejailla7431
@tejailla7431 4 жыл бұрын
This will works when duplicate elements exits in pairs
@syed7996
@syed7996 4 жыл бұрын
how you came up with this partition property and pair index property? (how you got that intuition). Please explain
@techdose4u
@techdose4u 4 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
Yea sure.
@saranshkumar4777
@saranshkumar4777 2 жыл бұрын
Simplest Approach :)
@guptasaurabh688
@guptasaurabh688 4 жыл бұрын
Which tool you are using with this pen to explain? Pls reply
@techdose4u
@techdose4u 4 жыл бұрын
Wacom pro
@punjabicodingstyle5111
@punjabicodingstyle5111 4 жыл бұрын
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.
@NikhilKumar-oy7mx
@NikhilKumar-oy7mx 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@techdose4u how does max IO work
@techdose4u
@techdose4u 4 жыл бұрын
By Max io I meant max io speed.
@BarkaDog
@BarkaDog 4 жыл бұрын
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.
@anmolsharma9539
@anmolsharma9539 2 жыл бұрын
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
@codeminatiinterviewcode6459
@codeminatiinterviewcode6459 4 жыл бұрын
Sir i am in a confusion of using low=mid+1, high = mid ; and taking while(low
@techdose4u
@techdose4u 4 жыл бұрын
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
@nick-sx2zn
@nick-sx2zn 2 жыл бұрын
what if there are theree 1s on left side
@gauravmishra8782
@gauravmishra8782 9 ай бұрын
Hey anyone what is purpose of checking the boundary element
@_ankush_verma
@_ankush_verma Жыл бұрын
I doubt that it won't work in case when some elements are triplets then pair property will not help[
@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
@techgamer1333
@techgamer1333 4 жыл бұрын
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 жыл бұрын
Nope. It will take O(N) because you need to visit all elements to count them.
@v.sreesairam9488
@v.sreesairam9488 3 жыл бұрын
bhaiya is it ok to do mid=(low+high)/2 inside the binary search
@techdose4u
@techdose4u 3 жыл бұрын
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👍👍
@yaswanthp2294
@yaswanthp2294 2 жыл бұрын
Is it allowed to have same element more than two
@apoorvbagal2936
@apoorvbagal2936 Жыл бұрын
No
@ashutoshshukla650
@ashutoshshukla650 4 жыл бұрын
if (nums[mid] == nums[mid ^ 1]) can handle even and odd
@techdose4u
@techdose4u 4 жыл бұрын
Yes you can use this or bitwise & as well.
@chandrukumar8131
@chandrukumar8131 4 жыл бұрын
return sum(set(nums))*2-sum(nums)
@chandrukumar8131
@chandrukumar8131 4 жыл бұрын
see this solution sir
@techdose4u
@techdose4u 4 жыл бұрын
This is O(N).
@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
@sonuagarwal7679
@sonuagarwal7679 4 жыл бұрын
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
@sahildigikar9115
@sahildigikar9115 4 жыл бұрын
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 жыл бұрын
Try to cover all top 20 questions from all ropics on geeksforgeeks. This should get you going.
@sahildigikar9115
@sahildigikar9115 4 жыл бұрын
@@techdose4u Okay thank you
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@yaswanthp2294
@yaswanthp2294 2 жыл бұрын
It only works when each element exactly appears twice, but one element appears exactly once Please clarify that
@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.
@navinchainani4721
@navinchainani4721 3 жыл бұрын
We can use xor its too easy
@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
@navinchainani4721
@navinchainani4721 3 жыл бұрын
We can solve by using xor also
@retrogame3138
@retrogame3138 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@techdose4u we could have used xor or array ans we will get the ans 2 line code
@swapnilsah3680
@swapnilsah3680 4 жыл бұрын
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.
@niranjanhegde1535
@niranjanhegde1535 4 жыл бұрын
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 жыл бұрын
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
@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 10 ай бұрын
U dint explian code
@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
@kushgupta1187
@kushgupta1187 4 жыл бұрын
public int singleNonDuplicate(int[] nums) { int low=0; int high=nums.length-1; int mid=0; while(low
Remove K digits | Build lowest number | Leetcode #402
15:30
Techdose
Рет қаралды 88 М.
Single Element in a Sorted Array - Leetcode 540 - Python
10:44
NeetCodeIO
Рет қаралды 19 М.
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 28 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 16 МЛН
Gym belt !! 😂😂  @kauermotta
00:10
Tibo InShape
Рет қаралды 18 МЛН
Задержи дыхание дольше всех!
00:42
Аришнев
Рет қаралды 3,7 МЛН
Single Element in a Sorted Array | Amazon | Microsoft | Leetcode 540
24:50
Search in rotated sorted array | Leetcode #33
13:52
Techdose
Рет қаралды 83 М.
Majority element in an array | Bitmasking
11:25
Techdose
Рет қаралды 16 М.
Find equilibrium point in an array
10:32
Techdose
Рет қаралды 54 М.
Jump game | Leetcode #55 | Valley peak approach
12:28
Techdose
Рет қаралды 185 М.
Trick for spiral matrix traversal
10:12
Techdose
Рет қаралды 199 М.
Contiguous array | Leetcode #525
13:12
Techdose
Рет қаралды 52 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 28 МЛН