I always love it when you start as a confused boy tries different approaches and then we realize we are even missing the wrong approaches to try and then slowly reach at right solution
Answer to the follow up: We need sorting first and I think for a given non distinct range, we decrement the first half elements and increment the rest or it's the other way around, increment first half elements and decrement all the other elements. For even range {1,1,1,1} the optimal ans would be {0,1,2,3} or {2,1,0,-1} which is 1+0+1+2=4. For odd range {1,1,1,1,1} the optimal would be 2+1+0+1+2. So, we find the mid and sum of 1 to n of length (mid-1) + sum of 1 to n (n-mid).
can someone please look into my code i used hashmap instead of count sort but it is giving me error in case: [3,2,1,2,1,7] class Solution { public: int minIncrementForUnique(vector& nums) { unordered_map map; for(auto i:nums) map[i]++; int moves=0; for(auto [val,freq]:map){ if(freq
U have used an unordered map, which means order is not maintained, so when u r iterating over the map, it could be random elements (keys), thus it will lead to incorrect answers.
actually we only maxE+2 for array size. +1 for oth and 1 extra index after maxEle. loop till maxEle...dump remaining in next index .then use n*(n+1)/2 formula for adding extra to result [ n = arr[maxEle+1] - 1 ]
I always love it when you start as a confused boy tries different approaches and then we realize we are even missing the wrong approaches to try and then slowly reach at right solution
wonderful explanation thanks !!
Very Relatable explanation, is perfect for newbies or beginners. You make the question very easy to understand
Answer to the follow up:
We need sorting first and I think for a given non distinct range, we decrement the first half elements and increment the rest or it's the other way around, increment first half elements and decrement all the other elements.
For even range {1,1,1,1} the optimal ans would be {0,1,2,3} or {2,1,0,-1} which is 1+0+1+2=4.
For odd range {1,1,1,1,1} the optimal would be 2+1+0+1+2.
So, we find the mid and sum of 1 to n of length (mid-1) + sum of 1 to n (n-mid).
crystal clear explanation :)
can someone please look into my code i used hashmap instead of count sort but it is giving me error in
case: [3,2,1,2,1,7]
class Solution {
public:
int minIncrementForUnique(vector& nums) {
unordered_map map;
for(auto i:nums) map[i]++;
int moves=0;
for(auto [val,freq]:map){
if(freq
U have used an unordered map, which means order is not maintained, so when u r iterating over the map, it could be random elements (keys), thus it will lead to incorrect answers.
@@rajrajesh1669 oo.. thanks..
Very good mittal
thankyou so much sir !!
Thank you! Actually we need the array of size = (max_el+1+arr_len-1) = (max_el+arr_len)
Can you tell the question you asked if we can decrease the element too
actually we only maxE+2 for array size.
+1 for oth and 1 extra index after maxEle.
loop till maxEle...dump remaining in next index
.then use n*(n+1)/2 formula for adding extra to result [ n = arr[maxEle+1] - 1 ]
maksat nhi bhul na
Aryan bro how many Goldman Sacks t-shirt you have, what I think you should conduct give-away one Goldman Sacks t-shits to your subscribers.
Cool, i got clear understanding on counting Sort this week!
Thanks Aryan 🫂
Can you tell the question you asked if we can decrease the element too