Single element in a sorted array | Leetcode

  Рет қаралды 56,197

Techdose

Techdose

Күн бұрын

Пікірлер: 174
@NeverGiveUp186
@NeverGiveUp186 4 жыл бұрын
Easy approach to toughest of the questions...This man is simply a legend !! 🙌🙌
@techdose4u
@techdose4u 4 жыл бұрын
😅
@shreyasvishwakarma8979
@shreyasvishwakarma8979 3 жыл бұрын
Shocked to see that you have around 90k subscribers . You deserve atleast 500k subscribers dude !
@techdose4u
@techdose4u 3 жыл бұрын
😊 Thanks
@TechBroQuiz
@TechBroQuiz 3 жыл бұрын
Finally understood this question after seeing four different videos.Thanks a lot🙏
@ankushreddy9789
@ankushreddy9789 3 жыл бұрын
oh yeah... this is easily the most intuitive of all the other videos for this question.
@nitesh2371
@nitesh2371 2 ай бұрын
it is most easy approach that i have seen on youtube,thanx
@techdose4u
@techdose4u 2 ай бұрын
welcome :)
@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 :)
@ajaygonepuri2285
@ajaygonepuri2285 Жыл бұрын
Great Explanation better than anyone on KZbin!!
@techdose4u
@techdose4u Жыл бұрын
:)
@wasimahmad9927
@wasimahmad9927 Жыл бұрын
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.
@oqant0424
@oqant0424 Жыл бұрын
u explained it so well........ u r simply a legend !! 🙌🙌
@techdose4u
@techdose4u Жыл бұрын
:)
@yitingg7942
@yitingg7942 3 жыл бұрын
Thanks a lot Surya! Couldn't think of this approach myself.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :) You are not replying now 🥺
@shivatomar4319
@shivatomar4319 4 жыл бұрын
Bhetreen samjhaya Bhai 👏🏼🙆🏻‍♂️
@techdose4u
@techdose4u 4 жыл бұрын
Dhanyawaad bhai :)
@saraswatirathore3933
@saraswatirathore3933 2 жыл бұрын
Wonderful explanation ,and I am thankful for your efforts which help me to understand the concept.
@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
@SirAkPandey
@SirAkPandey 2 жыл бұрын
Best explanation for this problem
@captain_vegan
@captain_vegan 2 жыл бұрын
Very helpfull thank you, u deserve million subs, man
@cristianouzumaki2455
@cristianouzumaki2455 2 жыл бұрын
Your explanations are gems. Thanks sir.
@ChapalArchive
@ChapalArchive 2 ай бұрын
explained beautifully , you earned a sub 👍
@techdose4u
@techdose4u 2 ай бұрын
thanks man :)
@prathaps2753
@prathaps2753 4 жыл бұрын
you are superb man!! keep making videos . Your channel helps a lot
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@gokulnaathbaskar9808
@gokulnaathbaskar9808 2 жыл бұрын
Thank you so much! Loved the pair index property
@letsdoeverythinginoneweek9398
@letsdoeverythinginoneweek9398 3 жыл бұрын
nice bro why i like u because my approach && my thinking ==your approach + your explanation
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@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
@gautamtyagi8846
@gautamtyagi8846 Жыл бұрын
thanks a lot, very easy to grasp ur explanation
@ashwinvarma9349
@ashwinvarma9349 4 жыл бұрын
Thats awesome man! Keep up the good work!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@rahulbhalla9891
@rahulbhalla9891 3 жыл бұрын
Please upload more and more videos. Your video is very helpful
@MJ-zs5jv
@MJ-zs5jv Жыл бұрын
Such a great explanation!
@mohammedyunusshaikh5080
@mohammedyunusshaikh5080 2 жыл бұрын
Thankyou Sir for the great explanation
@nayankhuman1043
@nayankhuman1043 2 жыл бұрын
Love your explanation ❤
@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.
@adityajain3664
@adityajain3664 Жыл бұрын
NIce intuitive solution.
@uv9111
@uv9111 2 жыл бұрын
Amazingly explained
@apoorvraizada
@apoorvraizada 3 жыл бұрын
The best explanation, Thank you so much for making great content.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@tejailla7431
@tejailla7431 4 жыл бұрын
This will works when duplicate elements exits in pairs
@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
@GhostRider....
@GhostRider.... 2 жыл бұрын
Nice explanation sir 🔥🔥
@chandrukumar8131
@chandrukumar8131 4 жыл бұрын
return sum(set(nums))*2-sum(nums)
@chandrukumar8131
@chandrukumar8131 4 жыл бұрын
see this solution sir
@techdose4u
@techdose4u 4 жыл бұрын
This is O(N).
@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.
@saranshkumar4777
@saranshkumar4777 2 жыл бұрын
Simplest Approach :)
@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.
@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
@krishnavamsinadharikatla5150
@krishnavamsinadharikatla5150 4 жыл бұрын
Good approach 👏👏
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@geekieyyy
@geekieyyy 2 жыл бұрын
awesome explanation bro
@TarunSharma-iv7iv
@TarunSharma-iv7iv 4 жыл бұрын
Great Explanation!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@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
@ambarishrishu
@ambarishrishu 2 жыл бұрын
nice video keep it up. do the solution in python. most people like me uses the python.
@priyanshukhullar5836
@priyanshukhullar5836 4 жыл бұрын
Awesome dada
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@vivek.tiwary
@vivek.tiwary 2 жыл бұрын
As usual, great explanation !!. Do you have any playlist for binary search?
@anurag_ad_01
@anurag_ad_01 Жыл бұрын
great solution
@nikhillingam4630
@nikhillingam4630 4 жыл бұрын
I'm waiting Hurrah!
@techdose4u
@techdose4u 4 жыл бұрын
:)
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Nice explanation!
@techdose4u
@techdose4u 4 жыл бұрын
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
@agileprogramming7463
@agileprogramming7463 4 жыл бұрын
Awesome as always!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@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.
@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.
@SmokyBigSmoke
@SmokyBigSmoke 4 жыл бұрын
Thank you so much.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@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
@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
@Harish-ll9tv
@Harish-ll9tv Жыл бұрын
Sir ,i done this problem like this int singleNonDuplicate(vector& nums) { int ans=0; for(int i=0;i
@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.
@sarthakchoudhary811
@sarthakchoudhary811 4 жыл бұрын
amazing sir.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@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.
@anmolsharma9539
@anmolsharma9539 3 жыл бұрын
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
@_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[
@hoperadas6010
@hoperadas6010 2 жыл бұрын
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
@nick-sx2zn
@nick-sx2zn 2 жыл бұрын
what if there are theree 1s on left side
@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
@punjabicodingstyle5111
@punjabicodingstyle5111 4 жыл бұрын
Amazing
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@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
@v.sreesairam9488
@v.sreesairam9488 4 жыл бұрын
bhaiya is it ok to do mid=(low+high)/2 inside the binary search
@techdose4u
@techdose4u 4 жыл бұрын
It depends. Just do a dry run and check.
@v.sreesairam9488
@v.sreesairam9488 4 жыл бұрын
Ok bhaiya thanks for your reply
@krishnakantahirwar6418
@krishnakantahirwar6418 3 жыл бұрын
It sometime gives you stack overflow otherwise it's okk👍👍
@guptasaurabh688
@guptasaurabh688 4 жыл бұрын
Which tool you are using with this pen to explain? Pls reply
@techdose4u
@techdose4u 4 жыл бұрын
Wacom pro
@santoshbhupati3579
@santoshbhupati3579 3 жыл бұрын
Thanks
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@sourabhajoshi2129
@sourabhajoshi2129 11 ай бұрын
Thank you
@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
@aniketbasu3865
@aniketbasu3865 2 жыл бұрын
bro you are awesome :)
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@gauravmishra8782
@gauravmishra8782 Жыл бұрын
Hey anyone what is purpose of checking the boundary element
@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
@yaswanthp2294
@yaswanthp2294 2 жыл бұрын
Is it allowed to have same element more than two
@apoorvbagal2936
@apoorvbagal2936 Жыл бұрын
No
@dhanashreegodase4445
@dhanashreegodase4445 3 жыл бұрын
Thankuuuuu
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😀
@navinchainani4721
@navinchainani4721 3 жыл бұрын
We can use xor its too easy
@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.
@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
@kidoo1567
@kidoo1567 Жыл бұрын
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
@sayanmaitra11
@sayanmaitra11 2 жыл бұрын
thanks
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 316 М.
Single Element in a Sorted Array - Leetcode 540 - Python
10:44
NeetCodeIO
Рет қаралды 23 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Remove K digits | Build lowest number | Leetcode #402
15:30
Techdose
Рет қаралды 90 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 860 М.
70% of Programmers can't solve this LeetCode Easy Question
7:32
Single Element in a Sorted Array | Amazon | Microsoft | Leetcode 540
24:50
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 107 М.
BS-8. Single Element in Sorted Array
22:16
take U forward
Рет қаралды 187 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 518 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 781 М.
Single Element in a Sorted Array | leetcode 540 | Hindi
14:00
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 209 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН