@fisherCoder your approach is valid but code may not correct. what happen in the input is [1,1] and k=2. your code will return true whereas it should return false. you missed to add size check in the first if condition. Let me know if i am wrong.
This was very well explained. Thank you! A lot of solutions also includes if (k > 30000) return false; at the top, which seems to lower the runtime. Why would it return false for this case?
Thanks very nice approach.Tell me why below code works and accepted by leetcode even when input array isn't sorted: public boolean containsNearbyDuplicate(int[] nums, int k) { int len = nums.length;
Very well explained, thank you for going through the examples!
Glad it was helpful!
@fisherCoder your approach is valid but code may not correct. what happen in the input is [1,1] and k=2. your code will return true whereas it should return false. you missed to add size check in the first if condition. Let me know if i am wrong.
This was very well explained. Thank you!
A lot of solutions also includes
if (k > 30000) return false;
at the top, which seems to lower the runtime. Why would it return false for this case?
Very good solution, very good wholesome video.
Glad it helped
is this the best case approach for this problem - Time complexity: O(n)
Space complexity: O(min(n, k))
Yes, We can do this in O(n) time and O(1) space.
And how will you approach the problem if the difference is to at least k instead of atmost k?
Thanks very nice approach.Tell me why below code works and accepted by leetcode even when input array isn't sorted:
public boolean containsNearbyDuplicate(int[] nums, int k)
{
int len = nums.length;
for(int i=0; i
Sleek Binary Search broo.. Why did I not thought of this before...
where do you work?
It will be much better if you start explaining algorithms than typing the code itself
Good call! Will do, thanks!