BS-8. Single Element in Sorted Array

  Рет қаралды 173,666

take U forward

take U forward

Күн бұрын

Пікірлер: 351
@takeUforward
@takeUforward Жыл бұрын
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 Жыл бұрын
Outstanding 😊
@sudhanshushekhar4222
@sudhanshushekhar4222 Жыл бұрын
Understood
@DeepakKumar-uq9js
@DeepakKumar-uq9js Жыл бұрын
Thanks bhai Ji😃
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
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 10 ай бұрын
Brute force Simplified: int n = nums.length; if(n == 1) return nums[0]; // O(n/2) for(int i = 1;i
@ratnadeepsaha7675
@ratnadeepsaha7675 Жыл бұрын
8 video at once. U r a legend for the community. Salute.
@yashmundada2483
@yashmundada2483 5 ай бұрын
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 4 ай бұрын
thanks buddy
@nehalchaudhary3319
@nehalchaudhary3319 3 ай бұрын
These made this ques just a piece of cake, thanks 👍🏻
@shambhaviaggarwal9977
@shambhaviaggarwal9977 Ай бұрын
Thanks
@PrakharSrivastava-k2d
@PrakharSrivastava-k2d Жыл бұрын
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.
@lakshyagupta9435
@lakshyagupta9435 Жыл бұрын
The way you explained the approach is just awesome.
@PrioritizingPeace
@PrioritizingPeace 4 ай бұрын
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 5 ай бұрын
wrote the code in one go, without any error, all the test cases passed, the satisfaction level is insane
@sayanpradhan1366
@sayanpradhan1366 4 ай бұрын
apne is question ka logic khud banaya tha ya Striver bhaiyya ka video dekhne ke bad kiya tha ?
@raghavmanish24
@raghavmanish24 10 ай бұрын
it's obvious sir , how one can not understand this simplest explanation.......thankuu so much sir
@adityanigam8373
@adityanigam8373 Жыл бұрын
Striver you are the best, you clear even the smallest doubt, I always had a doubt to whether to take low< high or low
@AdityaKumar-be7hx
@AdityaKumar-be7hx Жыл бұрын
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.
@YogeshKumar-px5bd
@YogeshKumar-px5bd Жыл бұрын
The solution is great. More focussed on writing clean and readable code but not so much intuitive at first.
@harshsolanki1058
@harshsolanki1058 Жыл бұрын
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 Жыл бұрын
would'nt it take o(n/2)??
@shibainu7500
@shibainu7500 8 ай бұрын
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)
@arijitroy8652
@arijitroy8652 10 ай бұрын
We can write the same for loop loop ie. for(i=1; i
@ujjawal_
@ujjawal_ Жыл бұрын
00:00 Problem Explanation 02:42 Bruteforce (Approach 1) 04:52 Edge Cases 05:45 Binary Search (Approach 2) 20:27 Code
@ratnadeepsaha7675
@ratnadeepsaha7675 Жыл бұрын
Good work 👍
@senseiAree
@senseiAree Жыл бұрын
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.
@toxaim9936
@toxaim9936 2 ай бұрын
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
@chiragsharma8905
@chiragsharma8905 Жыл бұрын
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.
@baderajesh23
@baderajesh23 6 ай бұрын
Eliminating left or right part based on even,odd logic is awesome :)
@Satya-g5t
@Satya-g5t Ай бұрын
I really appreciate your enthusiasm and solutions.
@neerajkumar-ts6om
@neerajkumar-ts6om Ай бұрын
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.
@bapanapallihemanth705
@bapanapallihemanth705 Жыл бұрын
You are the king 👑 of coding striver
@AbhishekSingh-cq2jx
@AbhishekSingh-cq2jx Жыл бұрын
understood better than Scaler paid cource really Thank You
@abhirajrohilla
@abhirajrohilla 10 ай бұрын
class Solution { public: int singleNonDuplicate(vector& nums) { int ans=0; for(int i=0;i
@aayushgakhar3525
@aayushgakhar3525 7 ай бұрын
it has complexity of n
@vijaynag7723
@vijaynag7723 11 ай бұрын
its really great series ,Thanks Striver. Aap nhi hote to humara kya hota !!!!!!!!!!!!!!!!
@Manasidas99
@Manasidas99 Жыл бұрын
Great video help me a lot I can't explain how much help it Thank you, sir.
@maaz.i7
@maaz.i7 2 ай бұрын
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
@Shunya_Advait
@Shunya_Advait Жыл бұрын
Understood Sir, thanks a lot for this amazing video.
@Vamshi9876
@Vamshi9876 9 ай бұрын
Thanks. Great way of explaining complex questions.
@MJBZG
@MJBZG 5 ай бұрын
another solution is take xor of all the elements, TC ---> O(N), SC = O(1)
@PankajSingh-kz7or
@PankajSingh-kz7or 4 ай бұрын
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
@charan123rams3
@charan123rams3 Жыл бұрын
Sir your really great and inspiring us to learn more about the coding,thank you so much 😢
@bhupendramaurya6587
@bhupendramaurya6587 Жыл бұрын
bhaiya, you are explaining the concept too good, Thank you so much.
@abhishekkumar-fe8lw
@abhishekkumar-fe8lw Жыл бұрын
We can also trim search further by putting left=2 , and right =n-3.
@sayakghosh5104
@sayakghosh5104 Жыл бұрын
Mind blowing explanation.
@chethanprabhu4475
@chethanprabhu4475 4 ай бұрын
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.
@sushantguria8820
@sushantguria8820 3 ай бұрын
Understood , amazing explanation as always.
@cinime
@cinime Жыл бұрын
Understood! So amazing explanation as always, thank you very very much for your effort!!
@djsanket25
@djsanket25 2 ай бұрын
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❤❤❤❤
@iamprateek3220
@iamprateek3220 7 ай бұрын
Another solution: int singleNonDuplicate(vector& arr) { int n= arr.size(); int l=0, r=n-1; if(n==1){ return arr[0]; } while(l
@gauravmasand
@gauravmasand 3 ай бұрын
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]
@udatyadeb101
@udatyadeb101 10 ай бұрын
understood, for java people who are getting stuck in 25th test case, instead of using == operator, use .equals and it will pass.
@tech1hutz
@tech1hutz Жыл бұрын
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
@TrendyGamer007
@TrendyGamer007 Жыл бұрын
@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 👍🏻👍🏻👍🏻.
@sufyainposharkar2080
@sufyainposharkar2080 15 күн бұрын
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]; } };
@yashraj5898
@yashraj5898 20 күн бұрын
understood ! great work buddy !
@ashishranjan8350
@ashishranjan8350 Жыл бұрын
Hey striver please upload rest of the videos in this series.
@mohdbilal2
@mohdbilal2 Жыл бұрын
simple brute force is take xor of all element with xorr=0; ultimately you will get single element
@takeUforward
@takeUforward Жыл бұрын
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.
@hiteshpanchal5772
@hiteshpanchal5772 Жыл бұрын
Striver bhai maza hi aa gaya ...............one request to you is plzz bhai upload video alternate day😍
@jeehub041
@jeehub041 Жыл бұрын
Sir series me maja aa raha .... Ab aage ka videos bhi upload kar do please🙏🙏🙏
@manavsingh5919
@manavsingh5919 Жыл бұрын
Thank you Striver...Understood everything
@prathmeshparab7046
@prathmeshparab7046 Жыл бұрын
Amazing explanation ❣❣
@58harshverma57
@58harshverma57 Жыл бұрын
Quite interesting question!
@AYJ959
@AYJ959 Жыл бұрын
Simple O(n) in java return arr.stream().reduce(0, (a,b) -> (a^b));
@rushidesai2836
@rushidesai2836 Ай бұрын
Weirdly good question.
@VasanthChoudary-uc5cz
@VasanthChoudary-uc5cz Жыл бұрын
you can also optimise the brute force by using two pointer technique.
@traymk
@traymk Жыл бұрын
Kya mast thumbnail hai.. 🔥
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation with dry run understood
@abid3801
@abid3801 9 ай бұрын
Striver one dumb question. When you started practicing DSA .Did you able to come up these tricks on your own at least for some?
@Thorfinnn1
@Thorfinnn1 24 күн бұрын
Never bro it take several times to build a avg logic
@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 Жыл бұрын
Yes but it will have T.C of O(N) and with binary search we get O(logN)
@pokigamerop
@pokigamerop Жыл бұрын
My O(1) solution: That Single element is me :)
@sandeep4915
@sandeep4915 2 ай бұрын
that trimming❤❤
@ayushgaurabh8604
@ayushgaurabh8604 Жыл бұрын
superb explanation
@rkgk5445
@rkgk5445 3 ай бұрын
The way you say Single 😀, I don't think it's your problem Sir... right now Although, kudos to your way of explanation...👏
@kingbadshah452
@kingbadshah452 9 ай бұрын
thanks striver understood everything
@inderjeet09
@inderjeet09 Жыл бұрын
Please bro make a playlist on bit manipulation also thats a very difficult topic for us . Only you can make that easy.
@chphanisimha7802
@chphanisimha7802 Жыл бұрын
Such an incredible work!!
@prayasgautam9595
@prayasgautam9595 11 ай бұрын
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.
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
UNDERSTOOD
@bhavyasrikanchi5753
@bhavyasrikanchi5753 Жыл бұрын
excellenttttt explaination..!!!!! Understood
@be-sreenu2202
@be-sreenu2202 5 ай бұрын
it might be possible using XOR but TC-O(N)
@sagarshah5341
@sagarshah5341 10 ай бұрын
MIND BLOWN AT @7:14 DAyummmmmmmm!!!!!!!
@atharva_g_vlogs
@atharva_g_vlogs 2 ай бұрын
UNDERSTOOD SIR
@rahulkumarbarik7584
@rahulkumarbarik7584 Жыл бұрын
That single element is me 🥲
@utsavseth6573
@utsavseth6573 Жыл бұрын
Shandaar.
@arpitamanderna9111
@arpitamanderna9111 Жыл бұрын
Hey striver !! please upload rest of the videos too!! you are the best!!
@mayankshakya9200
@mayankshakya9200 Жыл бұрын
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 Жыл бұрын
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.
@himaniupadhyay8201
@himaniupadhyay8201 Жыл бұрын
Thanks Lord Striver
@ashwath1914
@ashwath1914 Жыл бұрын
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
@aakashsharma780
@aakashsharma780 Жыл бұрын
Understood @striver ❤🙌🥳
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
Understood. Thanks a lot.
@jayantgahlot2653
@jayantgahlot2653 5 ай бұрын
Better Code: while(lo
@VenusaiYalamanchili
@VenusaiYalamanchili 6 ай бұрын
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 6 ай бұрын
count me also
@hidden_star14
@hidden_star14 3 ай бұрын
You're not alone. I feel so dumb, I can't tell you! 😭
@ShortsChachu
@ShortsChachu 2 ай бұрын
​@@hidden_star14Same bro 😢
@NazeerBashaShaik
@NazeerBashaShaik 7 ай бұрын
Understood, thank you.
@sunnypunia6485
@sunnypunia6485 Жыл бұрын
bhaiya please complete all lectures of all questions in a to z dsa as soon as possible it will be very helpful
@anweshandhara2059
@anweshandhara2059 2 ай бұрын
XORRing entire collection
@Bigg_boss_trolls
@Bigg_boss_trolls 9 күн бұрын
just awesome.
@abhinavkumar1976
@abhinavkumar1976 Жыл бұрын
Bhai ne 1 din me RONIN bna diya code studios par . Send more videos !
@hemantjagtap7340
@hemantjagtap7340 Жыл бұрын
Striver, when will you upload the remaining vedios of Binary Search playlist ?
@divyadwivedi1527
@divyadwivedi1527 Жыл бұрын
Plz upload rest of the video as soon as possible
@oyeesharme
@oyeesharme 3 ай бұрын
understood bhaiya
@mohitsingh13
@mohitsingh13 3 ай бұрын
Understood ❤
@Learnprogramming-q7f
@Learnprogramming-q7f 9 ай бұрын
Thank you Bhaiya
@shahbazaalam7559
@shahbazaalam7559 9 ай бұрын
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
@DeepakPatel-d5v
@DeepakPatel-d5v 7 ай бұрын
Thanks a lot Bhaiya
@AshmitIITH
@AshmitIITH 5 ай бұрын
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
@adbhutakalpniya
@adbhutakalpniya 5 ай бұрын
bhaiya bawal ho aap
@YourCodeVerse
@YourCodeVerse 11 ай бұрын
Understood✅🔥🔥
@bishalsarkar9205
@bishalsarkar9205 6 ай бұрын
Understood.
@sanbidrc
@sanbidrc 10 ай бұрын
Simpler solution: public int singleNonDuplicate(int[] nums) { int l = 0; int h = nums.length-1; while(l < h){ int m = l + (h-l)/2; if(m%2 == 0){ if(nums[m] == nums[m+1]){ l = m + 2; } else{ h = m; } } else{ if(nums[m] == nums[m-1]){ l = m + 1; } else{ h = m; } } } return nums[l]; }
@harshilpatel3205
@harshilpatel3205 9 ай бұрын
Understood sir 😇😊
BS-9. Find Peak Element
32:53
take U forward
Рет қаралды 199 М.
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 292 М.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 117 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,9 МЛН
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 501 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 193 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 15 М.
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 364 М.
BS-16. Kth Missing Positive Number | Maths + Binary Search
22:52
take U forward
Рет қаралды 159 М.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 117 МЛН