I wondered if the nums[index] = -nums[index] should be under an else statement, but when I tried that, in one of my test cases, it printed out one of the duplicate values twice.
Abs(nums[i]) can be added nd i think we should use set for storing the result instead of list because if any value present 4 times then this solution will add that particular value 2 times, which we don't need i think
I guess the problem description is misleading, declaring an additional array/collection is extra space, specially arrayList and the kind, since those structures always have a buffer for adding elements.
Why we need to take negative based indexing can u ckarify this as you said that it's an zero based indexing and nunbers atarts from 1 to n then why we are taking negative indexing can't we get without that ?
I've been interviewing lately, and I'm not encountering any of these "common questions" I'm seeing on UA-cam. They're all different, and they're all hard. My question today - just the question and the examples of solutions was 63 lines, and I had about 20 minutes to solve it. I am so sick of the IT industry!
This solution will give ArrayIndexOutOfBounds exception, if any value in the array is greater than or equal to total array length plus 2. For example, if this array had a value, say 15, then as per solution, 15 - 1, means 14th index and there is no 14th index in the array.
What a sleek solution, nice! Although I was thinking technically you are using extra space for the minus signs. If the constraints are that the array elements are positive, then you don't need an array of integers, you can do it with an array of unsigned integers, which would save you half your space. Then you can can use the other half for a bool array (or even a bit array which would be like 8-16 times more space effecient) and make it a more clear/readable solution using the same space. But yeah, that's probably more complicated to think of during an interview
I saw only a video of your "slipping-window" so far, and I can already tell you are a great person. concision is good.
Brilliantly simple implementation of permutation cycles, thanks for the walk through, due to that I was finally able to get it to click.
There is no way someone can come up with this solution in 30 minutes if they haven't seen it before lol
Lol agreed
All your explanations are wonderful. Thank You.
You are welcome!
how do you get the Approach in first time??
how to handle if any index value is more than the length of the array
I wondered if the nums[index] = -nums[index] should be under an else statement, but when I tried that, in one of my test cases, it printed out one of the duplicate values twice.
Please keep doing this stuff. I also try this stuff but in JavaScript. Thanks.
I definitely will, thanks for watching!
Please cover more popular problems , that have these kind of tricky ways to reduce space complexity. Really liked the way u solved this problem
I will try my best! Thank you for watching :)
on line 12, we can just add => result.add(nums[i])?
We can't do that because nums[i] could be a negative number since on line 13 we swap signs.
Abs(nums[i]) can be added nd i think we should use set for storing the result instead of list because if any value present 4 times then this solution will add that particular value 2 times, which we don't need i think
I guess the problem description is misleading, declaring an additional array/collection is extra space, specially arrayList and the kind, since those structures always have a buffer for adding elements.
Why we need to take negative based indexing can u ckarify this as you said that it's an zero based indexing and nunbers atarts from 1 to n then why we are taking negative indexing can't we get without that ?
Thanks a lot bro, you always have an excellent way of explaining solutions.
Simply use hashmap store key as num and value as count and incr val if already present in map and eventually return key which is greater than 2 val
No extra space allowed.
I've been interviewing lately, and I'm not encountering any of these "common questions" I'm seeing on UA-cam. They're all different, and they're all hard. My question today - just the question and the examples of solutions was 63 lines, and I had about 20 minutes to solve it. I am so sick of the IT industry!
What if the elements doesn't lie in the range of the 1 to size of array?
Then in that case what should be the most efficient approach?
In that case I would use a HashSet. You can add all of the numbers inside of the set and any numbers that fail to be added you know are dups!
It becomes simple if you sort the input array. I don't know why it's a medium question.
Very helpful.Thank you so much 🤗
This solution will give ArrayIndexOutOfBounds exception, if any value in the array is greater than or equal to total array length plus 2.
For example, if this array had a value, say 15, then as per solution, 15 - 1, means 14th index and there is no 14th index in the array.
It can't because of the constraints
Lots of love thank you
Thanks lot😊. Nice job your doing
Thank you!
Dude, You Rock!
Thanks Ethan! I appreciate you watching and commenting
amazing explanation !!!
Glad it was helpful!
So basically we flip the numbers as a sign to see if we have already been there.
I wonder without knowing the solution in advance, how many people are able to come up with this solution during the 30min interview??
Likely not very many, myself included!
I hope I will get offer because of you thank you in advance
What a sleek solution, nice! Although I was thinking technically you are using extra space for the minus signs. If the constraints are that the array elements are positive, then you don't need an array of integers, you can do it with an array of unsigned integers, which would save you half your space. Then you can can use the other half for a bool array (or even a bit array which would be like 8-16 times more space effecient) and make it a more clear/readable solution using the same space. But yeah, that's probably more complicated to think of during an interview
Amazing
Thank you!
Hi
couldnt u sort with runtime of (n log n) then run thru array comparing i - 1 with i and then add dupe to result array?
dont get me wrong, your's is cool, i was just curious.
time complexity has to be O(n)