BS-8. Single Element in Sorted Array

  Рет қаралды 122,638

take U forward

take U forward

Күн бұрын

Problem Link: bit.ly/42KKHj5
Notes/C++/Java/Python codes: takeuforward.org/data-structu...
We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
0:00 Introduction of Course

Пікірлер: 279
@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 11 ай бұрын
Thanks bhai Ji😃
@samuelfrank1369
@samuelfrank1369 9 ай бұрын
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 Жыл бұрын
8 video at once. U r a legend for the community. Salute.
@yashmundada2483
@yashmundada2483 Ай бұрын
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 11 күн бұрын
thanks buddy
@lakshyagupta9435
@lakshyagupta9435 8 ай бұрын
The way you explained the approach is just awesome.
@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.
@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
@cinime
@cinime Жыл бұрын
Understood! So amazing explanation as always, thank you very very much for your effort!!
@charan123rams3
@charan123rams3 Жыл бұрын
Sir your really great and inspiring us to learn more about the coding,thank you so much 😢
@raghavmanish24
@raghavmanish24 5 ай бұрын
it's obvious sir , how one can not understand this simplest explanation.......thankuu so much sir
@YogeshKumar-px5bd
@YogeshKumar-px5bd Жыл бұрын
The solution is great. More focussed on writing clean and readable code but not so much intuitive at first.
@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 👍
@Vamshi9876
@Vamshi9876 4 ай бұрын
Thanks. Great way of explaining complex questions.
@arijitroy8652
@arijitroy8652 5 ай бұрын
We can write the same for loop loop ie. for(i=1; i
@Manasidas99
@Manasidas99 9 ай бұрын
Great video help me a lot I can't explain how much help it Thank you, sir.
@bhupendramaurya6587
@bhupendramaurya6587 Жыл бұрын
bhaiya, you are explaining the concept too good, Thank you so much.
@chphanisimha7802
@chphanisimha7802 Жыл бұрын
Such an incredible work!!
@sayakghosh5104
@sayakghosh5104 Жыл бұрын
Mind blowing explanation.
@user-sd1ou1db1p
@user-sd1ou1db1p 11 ай бұрын
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.
@AbhishekSingh-cq2jx
@AbhishekSingh-cq2jx 9 ай бұрын
understood better than Scaler paid cource really Thank You
@Shunya_Advait
@Shunya_Advait 10 ай бұрын
Understood Sir, thanks a lot for this amazing video.
@bapanapallihemanth705
@bapanapallihemanth705 Жыл бұрын
You are the king 👑 of coding striver
@manavsingh5919
@manavsingh5919 10 ай бұрын
Thank you Striver...Understood everything
@prathmeshparab7046
@prathmeshparab7046 11 ай бұрын
Amazing explanation ❣❣
@vijaynag7723
@vijaynag7723 6 ай бұрын
its really great series ,Thanks Striver. Aap nhi hote to humara kya hota !!!!!!!!!!!!!!!!
@58harshverma57
@58harshverma57 11 ай бұрын
Quite interesting question!
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation with dry run understood
@bhavyasrikanchi5753
@bhavyasrikanchi5753 Жыл бұрын
excellenttttt explaination..!!!!! Understood
@samuelfrank1369
@samuelfrank1369 9 ай бұрын
Understood. Thanks a lot.
@baderajesh23
@baderajesh23 2 ай бұрын
Eliminating left or right part based on even,odd logic is awesome :)
@ayushgaurabh8604
@ayushgaurabh8604 11 ай бұрын
superb explanation
@NazeerBashaShaik
@NazeerBashaShaik 3 ай бұрын
Understood, thank you.
@kingbadshah452
@kingbadshah452 5 ай бұрын
thanks striver understood everything
@harshsolanki1058
@harshsolanki1058 9 ай бұрын
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 7 ай бұрын
would'nt it take o(n/2)??
@shibainu7500
@shibainu7500 3 ай бұрын
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)
@hiteshpanchal5772
@hiteshpanchal5772 11 ай бұрын
Striver bhai maza hi aa gaya ...............one request to you is plzz bhai upload video alternate day😍
@vigneshwaran_2002
@vigneshwaran_2002 Жыл бұрын
Thankyou sir very helpful❤
@shivajirao999
@shivajirao999 Ай бұрын
wrote the code in one go, without any error, all the test cases passed, the satisfaction level is insane
@sayanpradhan1366
@sayanpradhan1366 9 күн бұрын
apne is question ka logic khud banaya tha ya Striver bhaiyya ka video dekhne ke bad kiya tha ?
@chiragsharma8905
@chiragsharma8905 8 ай бұрын
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.
@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
@jeehub041
@jeehub041 Жыл бұрын
Sir series me maja aa raha .... Ab aage ka videos bhi upload kar do please🙏🙏🙏
@utsavseth6573
@utsavseth6573 Жыл бұрын
Shandaar.
@yogeeshrsyogeeshrs4204
@yogeeshrsyogeeshrs4204 11 ай бұрын
Understood ❤🎉
@YourCodeVerse
@YourCodeVerse 6 ай бұрын
Understood✅🔥🔥
@user-or5oz1pk2x
@user-or5oz1pk2x 2 ай бұрын
Thanks a lot Bhaiya
@abhay9994
@abhay9994 8 ай бұрын
Thank You.
@kiranmoura2974
@kiranmoura2974 Жыл бұрын
Understood ❤
@harshilpatel3205
@harshilpatel3205 5 ай бұрын
Understood sir 😇😊
@khalasianiket816
@khalasianiket816 14 күн бұрын
understood ❤
@hallupandet228
@hallupandet228 Жыл бұрын
Understood 🤗
@UserUser-tn8tv
@UserUser-tn8tv 5 ай бұрын
Understood!
@soumi6720
@soumi6720 4 ай бұрын
understood!!
@aakashsharma780
@aakashsharma780 Жыл бұрын
Understood @striver ❤🙌🥳
@RAJSINGH-mr7hq
@RAJSINGH-mr7hq Жыл бұрын
Superb 👌
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
UNDERSTOOD
@aneesahmad05
@aneesahmad05 5 ай бұрын
Excellent
@adityarajsrivastava3291
@adityarajsrivastava3291 28 күн бұрын
understood!
@senseiAree
@senseiAree 9 ай бұрын
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.
@rakshanaravindran9809
@rakshanaravindran9809 9 ай бұрын
Understood!!
@bishalsarkar9205
@bishalsarkar9205 Ай бұрын
Understood.
@ashishranjan8350
@ashishranjan8350 Жыл бұрын
Hey striver please upload rest of the videos in this series.
@mbm.editzz
@mbm.editzz Жыл бұрын
thank you sir
@user-sm7zo5zd9t
@user-sm7zo5zd9t 4 ай бұрын
Understood
@traymk
@traymk Жыл бұрын
Kya mast thumbnail hai.. 🔥
@pihus3498
@pihus3498 Жыл бұрын
Understoooood
@rajeswarichalamcherla2860
@rajeswarichalamcherla2860 Жыл бұрын
Understood !!!!
@anshujaiswal5622
@anshujaiswal5622 3 ай бұрын
Understood :)
@dreamyme543
@dreamyme543 9 ай бұрын
Understood🙃
@sulthan6131
@sulthan6131 Жыл бұрын
Love u lots❤
@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 👍🏻👍🏻👍🏻.
@CoderGrow1
@CoderGrow1 5 ай бұрын
thanks sir
@abhishekkumar-fe8lw
@abhishekkumar-fe8lw 9 ай бұрын
We can also trim search further by putting left=2 , and right =n-3.
@sunnypunia6485
@sunnypunia6485 Жыл бұрын
bhaiya please complete all lectures of all questions in a to z dsa as soon as possible it will be very helpful
@soumiyamuthuraj3516
@soumiyamuthuraj3516 13 күн бұрын
awesome
@user-js1rx8rs9p
@user-js1rx8rs9p 2 ай бұрын
understood bro 😎😎😎
@pranavmisra5870
@pranavmisra5870 5 ай бұрын
understood😁
@sagarshah5341
@sagarshah5341 6 ай бұрын
MIND BLOWN AT @7:14 DAyummmmmmmm!!!!!!!
@abhirajrohilla
@abhirajrohilla 5 ай бұрын
class Solution { public: int singleNonDuplicate(vector& nums) { int ans=0; for(int i=0;i
@aayushgakhar3525
@aayushgakhar3525 2 ай бұрын
it has complexity of n
@arpitamanderna9111
@arpitamanderna9111 Жыл бұрын
Hey striver !! please upload rest of the videos too!! you are the best!!
@inderjeet09
@inderjeet09 Жыл бұрын
Please bro make a playlist on bit manipulation also thats a very difficult topic for us . Only you can make that easy.
@shanmukhavuyyuri4414
@shanmukhavuyyuri4414 10 ай бұрын
vere level
@chethanprabhu4475
@chethanprabhu4475 6 күн бұрын
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.
@user-kx6fi8ou9f
@user-kx6fi8ou9f 27 күн бұрын
bhaiya bawal ho aap
@suryasaimaheswar8636
@suryasaimaheswar8636 10 күн бұрын
understood:)
@Shivi32590
@Shivi32590 12 күн бұрын
thanku
@divyadwivedi1527
@divyadwivedi1527 Жыл бұрын
Plz upload rest of the video as soon as possible
@himaniupadhyay8201
@himaniupadhyay8201 10 ай бұрын
Thanks Lord Striver
@princebhanushali6255
@princebhanushali6255 Ай бұрын
understood.
@user-eq6ee8rd2h
@user-eq6ee8rd2h 11 ай бұрын
understood
@seemameghasai6744
@seemameghasai6744 11 ай бұрын
@takeUforward ,Since first two and last indices are same you can do low = 2 and high = arr.length-3 right?
@deepakojha3216
@deepakojha3216 9 ай бұрын
yes
@VINAYSINGH-wc8sq
@VINAYSINGH-wc8sq Жыл бұрын
🔥🔥🔥
@rahulkumarbarik7584
@rahulkumarbarik7584 9 ай бұрын
That single element is me 🥲
@VasanthChoudary-uc5cz
@VasanthChoudary-uc5cz 7 ай бұрын
you can also optimise the brute force by using two pointer technique.
@AYJ959
@AYJ959 Жыл бұрын
Simple O(n) in java return arr.stream().reduce(0, (a,b) -> (a^b));
@MJBZG
@MJBZG 16 күн бұрын
another solution is take xor of all the elements, TC ---> O(N), SC = O(1)
@ashishpradhan6250
@ashishpradhan6250 Ай бұрын
understood😮‍💨😮‍💨
@udatyadeb101
@udatyadeb101 6 ай бұрын
understood, for java people who are getting stuck in 25th test case, instead of using == operator, use .equals and it will pass.
@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
@anupkhismatrao9280
@anupkhismatrao9280 11 ай бұрын
❤❤❤
@adarshrajjaiswal9143
@adarshrajjaiswal9143 9 ай бұрын
My O(1) solution: That Single element is me :)
@NavyaVedachala
@NavyaVedachala Жыл бұрын
Is it possible to access the previous version of the A2Z DSA sheet? Links to problems and some videos are missing. There are also five problems missing. Is it possible to access these problems on a Google sheet if you have one until the glitch is fixed? Thank you! Loving the series
@takeUforward
@takeUforward Жыл бұрын
5 problems are back now
@SibiRanganathL
@SibiRanganathL 4 ай бұрын
Understood but need to revise.
BS-9. Find Peak Element
32:53
take U forward
Рет қаралды 137 М.
3M❤️ #thankyou #shorts
00:16
ウエスP -Mr Uekusa- Wes-P
Рет қаралды 14 МЛН
BS-10. Finding Sqrt of a number using Binary Search
17:11
take U forward
Рет қаралды 100 М.
I Parsed 1 Billion Rows Of Text (It Sucked)
39:23
Theo - t3․gg
Рет қаралды 90 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 237 М.
Two Sum | LeetCode 1 | JavaScript | Easy
13:20
Gordon Zhu
Рет қаралды 8 М.
Single element in a sorted array | Leetcode #540
8:26
Techdose
Рет қаралды 53 М.
Count Inversions in an Array | Brute and Optimal
24:17
take U forward
Рет қаралды 170 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 616 М.