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?
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!
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!
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.
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
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)
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.
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.
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.
@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 👍🏻👍🏻👍🏻.
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.
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]
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 ...
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
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
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.
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
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
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 ❤❤
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!
Outstanding 😊
Understood
Thanks bhai Ji😃
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?
Brute force Simplified:
int n = nums.length;
if(n == 1) return nums[0];
// O(n/2)
for(int i = 1;i
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!
thanks buddy
These made this ques just a piece of cake, thanks 👍🏻
8 video at once. U r a legend for the community. Salute.
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!
We can write the same for loop loop ie. for(i=1; i
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.
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
would'nt it take o(n/2)??
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)
The way you explained the approach is just awesome.
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.
Striver you are the best, you clear even the smallest doubt, I always had a doubt to whether to take low< high or low
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.
The solution is great. More focussed on writing clean and readable code but not so much intuitive at first.
it's obvious sir , how one can not understand this simplest explanation.......thankuu so much sir
00:00 Problem Explanation
02:42 Bruteforce (Approach 1)
04:52 Edge Cases
05:45 Binary Search (Approach 2)
20:27 Code
Good work 👍
Striver one dumb question. When you started practicing DSA .Did you able to come up these tricks on your own at least for some?
Eliminating left or right part based on even,odd logic is awesome :)
class Solution {
public:
int singleNonDuplicate(vector& nums) {
int ans=0;
for(int i=0;i
it has complexity of n
wrote the code in one go, without any error, all the test cases passed, the satisfaction level is insane
apne is question ka logic khud banaya tha ya Striver bhaiyya ka video dekhne ke bad kiya tha ?
another solution is take xor of all the elements, TC ---> O(N), SC = O(1)
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.
@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 👍🏻👍🏻👍🏻.
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.
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
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]
understood better than Scaler paid cource really Thank You
Understood , amazing explanation as always.
We can also trim search further by putting left=2 , and right =n-3.
Great video help me a lot I can't explain how much help it Thank you, sir.
Hey striver please upload rest of the videos in this series.
Another solution:
int singleNonDuplicate(vector& arr)
{
int n= arr.size();
int l=0, r=n-1;
if(n==1){
return arr[0];
}
while(l
Sir your really great and inspiring us to learn more about the coding,thank you so much 😢
You are the king 👑 of coding striver
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 ...
yes bhaiya, plz make one on greedy
Understood Sir, thanks a lot for this amazing video.
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
Thanks. Great way of explaining complex questions.
bhaiya, you are explaining the concept too good, Thank you so much.
Mind blowing explanation.
its really great series ,Thanks Striver. Aap nhi hote to humara kya hota !!!!!!!!!!!!!!!!
Understood! So amazing explanation as always, thank you very very much for your effort!!
you can also optimise the brute force by using two pointer technique.
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
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.
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
Please bro make a playlist on bit manipulation also thats a very difficult topic for us . Only you can make that easy.
Understood
Simple O(n) in java
return arr.stream().reduce(0, (a,b) -> (a^b));
Quite interesting question!
Thank you Striver...Understood everything
Striver bhai maza hi aa gaya ...............one request to you is plzz bhai upload video alternate day😍
Sir series me maja aa raha .... Ab aage ka videos bhi upload kar do please🙏🙏🙏
UNDERSTOOD
it might be possible using XOR but TC-O(N)
understood, for java people who are getting stuck in 25th test case, instead of using == operator, use .equals and it will pass.
Understood ❤
The way you say Single 😀, I don't think it's your problem Sir... right now
Although, kudos to your way of explanation...👏
simple brute force is take xor of all element with xorr=0;
ultimately you will get single element
Yes but the reason of saying this brute force was to explain the thought process of binary search
@@takeUforward Okay, Thanks brother 🥰 You are doing a great work.
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?
count me also
You're not alone.
I feel so dumb, I can't tell you! 😭
Such an incredible work!!
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.
excellenttttt explaination..!!!!! Understood
@takeUforward ,Since first two and last indices are same you can do low = 2 and high = arr.length-3 right?
yes
Good explanation with dry run understood
Understood, thank you.
thanks striver understood everything
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
My O(1) solution:
That Single element is me :)
Understood. Thanks a lot.
MIND BLOWN AT @7:14 DAyummmmmmmm!!!!!!!
what if there was 3 instead of 4 in the last example? If both at sides there were 3 then how would the search have operated?
I was searching for the same
Understood!
Understood, but your code can be made short as
int singleNonDuplicate(vector& a)
{
int n=a.size();
int l=0,r=n-1;
while(l
understood!
How can we do it through binary search if there are two single elements in an array ?
Understood.
Amazing explanation ❣❣
superb explanation
understood ❤
bhaiya please complete all lectures of all questions in a to z dsa as soon as possible it will be very helpful
Hey striver !! please upload rest of the videos too!! you are the best!!
Understood✅🔥🔥
Plz upload rest of the video as soon as possible
Striver, when will you upload the remaining vedios of Binary Search playlist ?
Shandaar.
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
5 problems are back now
Understood ❤🎉
That single element is me 🥲
Understood but need to revise.
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 ❤❤
Yes but it will have T.C of O(N) and with binary search we get O(logN)
Understood sir 😇😊
Understood :)
Thank you Bhaiya
Thank You.
Kya mast thumbnail hai.. 🔥
understood bhaiya
Understood @striver ❤🙌🥳
What happens if there are not exactly 2 elements which are duplicates? What if there are more than 2, in this case how can we identify the sequence?
Understood
similar simple code in c++
class Solution {
public:
int singleNonDuplicate(vector& nums) {
int s = 0;
int e = nums.size() - 2;
while (s
Understoooood
thanks sir