I think most people would be afraid to jump +2 or -2 from mid like that, in case of going outside the bounds of the array. Going through two example test cases with array lengths 1 and 3 would be a good idea to show that this is actually always safe.
@vedantshinde22773 жыл бұрын
Yes! You nailed the intuition...we essentially need to see if the subarray is "really" odd.
@AlgosWithMichael3 жыл бұрын
Yep exactly!
@mananvarma59443 жыл бұрын
This was a great intuitive solution, thanks!
@AlgosWithMichael3 жыл бұрын
Glad it was helpful!
@rkk19903 жыл бұрын
How do one remember such techniques during the interview ?
@devkunjadia37922 жыл бұрын
I love your content. Keep up the great work!
@bimerinoel49138 ай бұрын
that's very cool. i got something simple also, check this: public static int singleElement(int[] arr) { if (arr.length == 0) return 0; for (int i = 0; i < arr.length; i++){ if (i == arr.length-1) return arr[arr.length-1]; if (arr[i] == arr[i+1]) i++; else return arr[i]; } return 0; }
@surbhiagarwal23228 ай бұрын
Good solution. I thought of the same at first, but worst case is O(n) --> element at the last.
@TheRomanianWolf5 ай бұрын
@@surbhiagarwal2322 O(N), both solutions will be rejected.
@haoooyuliu68574 жыл бұрын
nice explanation, really help for this question!
@AlgosWithMichael4 жыл бұрын
Glad it helped you, thank you for watching!
@jamesyoo674 жыл бұрын
FYI I don't think this works when the last element is the unique number
@vasumahalingam51624 жыл бұрын
Ifs should check the boundaries; mid-1 >= 0 and nums[mid] == nums[mid-1] and mid + 1 < len(nums) and nums[mid] == nums[mid+1]
@AlgosWithMichael4 жыл бұрын
I verified the solution works with the following input: [1,1,2,2,3,3,4,4,8]. The reason why it works is because the if checks check mid - 1 and mid + 1. This problem is pretty tricky because of all the edge cases haha. Thanks for watching!
@AlgosWithMichael4 жыл бұрын
@@vasumahalingam5162 Yes, true!
@abhinavsingh-zc2hk3 жыл бұрын
Thank You so much for this amazing technique.
@AlgosWithMichael3 жыл бұрын
Most welcome 😊
@sachinmamadapur30004 жыл бұрын
Thanks for Nice Explanation :)
@AlgosWithMichael4 жыл бұрын
No problem, thank you for watching!
@sivan2878 Жыл бұрын
Good explanation bro. cool
@ankoor3 жыл бұрын
Such a great explanation...
@AlgosWithMichael3 жыл бұрын
Thank you!
@just_another_curious_guy4 жыл бұрын
Clear explanation, Thank you
@AlgosWithMichael4 жыл бұрын
You are welcome!
@heisenberg18444 жыл бұрын
Brilliant. Cheers mate.
@AlgosWithMichael4 жыл бұрын
No problem 👍
@swarnimvarshneya69448 ай бұрын
Hey.. I really can't get why after the second condition mid = mid+1 why it's the opposite. pl help anyone.
@surbhiagarwal23228 ай бұрын
Ok, line 13. That's because after we determine the subarray is even, in the second condition the duplicate of mid is included in the right side. When we know that the duplicate is in the right side and still the right subarray is odd, that means the one single element is on the right side.
@afk122175 ай бұрын
I used a different approach where I calculated the frequency of each element and checked it during each iteration and if it was not 2 then exited the loop and displayed that number....but I could not meet the runtime🤕🤕
@AyaGamal20104 ай бұрын
There is a little better approach than Frequency, according to the sorted array so that you can do a linear search it will be a little better, but binary search is the best of them
@arpit_singh104 жыл бұрын
Amazing explanation
@AlgosWithMichael4 жыл бұрын
Glad it was helpful!
@yitingg79424 жыл бұрын
Michael, can you please please do one for 93. Restore IP Addresses?
@AlgosWithMichael4 жыл бұрын
Definitely!
@vishnugajulapalli53053 жыл бұрын
What is the white board that you are using?
@AlgosWithMichael3 жыл бұрын
awwapp.com/
@riyazbasha7982 Жыл бұрын
Can do it for operation
@code74344 жыл бұрын
Nice Explanation :)
@AlgosWithMichael4 жыл бұрын
Thank you!
@bismeetsingh3524 жыл бұрын
What do we do when both sides have odd no of elements?
@AlgosWithMichael4 жыл бұрын
The logic should be the same if the entire array is odd
@stlo03092 жыл бұрын
i simply went for linear search lol
@antarikshjain6802 Жыл бұрын
bhai thanks
@psn9991004 жыл бұрын
AT this TS kzbin.info/www/bejne/anjMaah3r5tpbNU If we know that mid and mid + 1 are same, then we can check for evenness on the left side of mid, Similarly , as you explained , when mid and mid - 1 are the same, we can check for evenness on the right hand side of mid . That would avoid calculation for the evenness only on the right hand side by either doing numOfElements - 1 then % 2 or numOfElements % 2 without a -1 . Does that make sense ? Here is the code that passed Leetcode OJ int singleNonDuplicate(vector& nums) { int n = nums.size(); int low = 0; int high = n - 1; while(low < high){ int mid = low + ((high - low) >> 1); //If mid is itself the number we are looking for, return nums[mid] if(nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) return nums[mid]; if((mid - 1) >= 0 && nums[mid] == nums[mid - 1]){ int numElementsInRHS = high - mid; /*If the num of Elements in the RHS is even, then we know that our single elemnet cannot lie in that half*/ if(!(numElementsInRHS & 1)){ high = mid - 2; }else{ low = mid + 1; } }else if ( (mid + 1 < n && nums[mid] == nums[mid + 1])){ int numElementsInLHS = mid - low; if(!(numElementsInLHS & 1)){ low = mid + 2; }else{ high = mid - 1; } } } return nums[low]; }
@AlgosWithMichael4 жыл бұрын
Yes it does, good catch! This problem is challenging because it has several edge cases I think.