Master Data Structures & Algorithms For FREE at AlgoMap.io!
@arpitkumargahlot
7 ай бұрын
Hi Greg, you are a real life-saver with your shorts. I'm really thankful for these uploads. I've been struggling with competitive coding (though I can do software development without issues). Your shorts and specially the solutions in Python have been really helpful for me. Please keep creating more of this content!!
@vafasadrif12
7 ай бұрын
You're right about the hashmap but to count the good pairs for each frequency, the actual formula is n*(n-1)/2 not just adding or subtracting 1
@01epoch74ny
7 ай бұрын
There's a mathematical solution for this, if the frequency is 1 then there are 0 good pairs, 2 then 1, 3 then 3, 4 then 6, 5 then 10, 6 then 15 and so on. With that in mind, 1 * 0 = 0; 2 * 0.5 = 1; 3 * 1 = 3; 4 * 1.5 = 6; 5 * 2 = 10; 6 * 2.5 = 15 and so on. The multiplier increase by 0.5 every time, to get the multiplier, you would do (frequency - 1) * 0.5. That got me to this answer: count = Counter(nums) return int(sum([(freq - 1) * 0.5 * freq for freq in count.values()]))
@arkapravadas8674
7 ай бұрын
Yes thought the same
@iDontProgramInCpp
7 ай бұрын
Sounds like a hack. What you're looking for is the amount of combinations of size K in a set of size N, which is n! / k!(n-k)!, noted as a parenthesis block with two stacked numbers, above K and below N.
@01epoch74ny
7 ай бұрын
it's the most optimal solution
@Ibra_
7 ай бұрын
It's like the mesh network topology where the number of wires is (n*(n-1)/2) where n is the number of nodes, and here, the n is the number of occurrences for each number
@aadilgoyal9286
7 ай бұрын
this can be proved by the following way - if the same number occurs on indexes 0, 2 and 5 then when counting from the end 5 can form with 2 indexes -> 0 or 2 2 with 1 -> 0 so it will be n-1 + n-2 + .... + 1 + 0 where n is the number of occurrences which is equal to n*(n-1)/2
@code2compass
7 ай бұрын
Greg my brother trust me you deserve more views.... Your channel should be in millions...
@GregHogg
7 ай бұрын
It is, I have like 12 million views total! I really appreciate the positivity 😊😊
@code2compass
7 ай бұрын
@@GregHogg no no no that's too less trust me I had 0 knowledge about dsa and whenever I used to see a word problem my mind was all 🦗🦗🦗🦗 But after when I started watching your vids at least İ can now read the problems. P.S I have a physics background with no traditional coding lessons even touched during my university. I started coding in MATLAB during my university and now I've switched to python last year and absolutely loving it.
@tarunganjoo8366
7 ай бұрын
Keep uploading these kind of videos
@iDontProgramInCpp
7 ай бұрын
The mathematical solution is as follows: For each number, store its count. Then sum the number of combinations of 2 elements from a set of that size. For example, in the example [1,2,3,1,1,3], you have three of 1 one of 2 and two of 3. We note (k, n) = n! / k!(n-k)! as long as 0≤k≤n, and 0 otherwise. So then R = (2, 3) + (2, 2) + (2, 1) = 3 + 1 + 0 = 4.
@mushfiqurrahman3803
7 ай бұрын
Another possible solution could be taking the frequency of each number and it's garunteed that for frequency n there are ((1+(n-1))*(n-1))/2 number of pairs possible, just add them up for all the numbers and You're good, it also solves it in 0(N) with hashmap, and. If u are using map as in c++ it will be NlogN where N defines no of unique numbers present in the array
@manas_thakre
7 ай бұрын
Your hashmap approach is wrong there is a test case in the question where nums = [1,1,1,1] so according to your solution it should return 3 but the answer is 6. According to me first sorting the array then using two pointers i
@weter1231
7 ай бұрын
I think you misunderstood the Hash map solution because he did not continue with the example. for your case, the hash map will be {1:4}. after creating the hash map, you have to loop through the array. in each iteration, based on they key ,a value will be subtracted by one then the result will be added to the goodPairs variable. in your test case, it will be 3 + 2 + 1 which result to 6. I had to rewatch the Short several times to figure it out.
@manas_thakre
7 ай бұрын
@@weter1231 Now I get it Thanks for clarifying.
@ikoiko6540
7 ай бұрын
Solution with javascript: const arr=[1,2,3,1,1,3]; function GoodPairs(arr){ const freq={}; let sum=0; for(let i=0;i
@birphon
7 ай бұрын
heyhey, is there a place where its accessable to get these FAANG questions? I don't intend to work at one of these locations but thought it might be a good thing to do a question a day though googling for faang interview questions im always getting "generic coding interview questions" i.e. whats an array
@InfernalJoy
7 ай бұрын
Thanks, but how do we return the pairs, instead of just the count?
@FeLiNe418
7 ай бұрын
Why not create a counter as you go the the num list? If num is not in the dictionary: add it to the dictionary with value 1. Else: increment good_pairs counter by the num's dictionary value and then increment the dictionary value by 1.
@RolloCasanova
7 ай бұрын
Where does the last pair comes from? Following the hash map solution, I don't understand how 2+1 equals 4.
@GregHogg
7 ай бұрын
Sorry I ran out of time and didn't run through the full solution
@urbsrocher
7 ай бұрын
@@GregHogg Why not explain it here then? Because I dont think this works. For each entry in nums, you need to do nums[i]!/2! * nums[i]-2
7 ай бұрын
you can't understand it because idea and code is wrong - it should add sum(range(num)) and without substracting 1 (*** correction: I forgot .values() in previous version ***) for n in nums.values(): good_pairs += sum(range(n)) 2 elements can create 1 pair 3 elements can create 2+1 pairs 4 elements can create 3+2+1 pairs N elements can create (N-1)+... +2+1 pairs
@TargunYssboern
7 ай бұрын
@@GregHogg Then what's the point of this short? It shows (unfinished) solution without explaining underlaying mathematical principle - C(n,2) which has its own simple algorithm. Is your way of learning to 'memorize solution without understanding'?
@travelan
7 ай бұрын
This is a great example of why Big O notation is useless in practice. Just solve everything with a hashmap and all algorithms will solve in O(1)!
@alexanderlea2293
7 ай бұрын
Couldn't you just get the list of counts and take (n choose 2) for each count?
@1vladbara
7 ай бұрын
Yes, exactly is Combinations of n out of 2. C(n,2)
@davea136
6 ай бұрын
This is correct. and there's a way to do it easily - math.comb(n, r) for (n choose r). Use the existing libs if you can.
@supun_sandaruwan
7 ай бұрын
your channel is awesome ❤
@GregHogg
7 ай бұрын
Thank you so much!!!
@EMdragonKnight
7 ай бұрын
Almost what I thought, but in my head I was thinking check if it exists see how many with arr[i] being the key and value being instances. No subtraction just when you find match increment answer by instances. Then increment dictionary value by one.
@tatebuckland5241
7 ай бұрын
Wouldnt creating a hashmap with the occurrence be o(n) time making it not efficient
@tatebuckland5241
7 ай бұрын
Sorry o(n^2)
@B_Y_T_E
6 ай бұрын
What the website u using called?
@AshishRai-gw7sh
7 ай бұрын
Not sure if your solution will work.My suggestion , need to start from back of the array then if number doesn't exist in hashmap then insert it else take the current count of number to final result and increment the count. This way you will find correct count of pairs
@EMdragonKnight
7 ай бұрын
I don't think starting from the end really makes a difference. It's basically the same forward or backwards.
@ProfRoxas
7 ай бұрын
After some rewatch i managed to somehow get the method, but I'm failing to understand how making a hashmap is better solution than a nested loop.l, mqybe with large ammont of data (like i think nested is (n*n)/2 while hashmap is like 2n, one for makind and one for counting)
@JadaTheSupreme
7 ай бұрын
The nested loop has a time complexity of N^2, whereas the hash map solution cuts this down to just N. It’s a pretty huge improvement.
@arsheyajain7055
7 ай бұрын
Great explanation 😊
@GregHogg
7 ай бұрын
Thanks!
@usemayonaise
7 ай бұрын
2+0+1=3 and it says the answer is 4... did i miss something?
@189sharik
6 ай бұрын
I did not understand why did you use enumerate ? While you never used i anywhere in the code.
@ashraf_isb
7 ай бұрын
hey this is relatable to bucket sort, i think problem 347
@euginading2144
7 ай бұрын
(1,1,1,1) is (1,1,1)+3, where (1,1,1) is 3, so total is 6, is the sum of freq. hash map, where each is freq=1:0, 2:1, 3:3, 4:6
@SeRoShadow
7 ай бұрын
Is this alg even practical ? if so, give a real-life example in which this alg would be one of / the only / the best solution. Thats the question I have in mind when seeing these. Its like asking a common folk what are imaginary numbers useful for ? Maybe rocket since, but im pretty sure that company aint doing that on daily basis.
7 ай бұрын
Code is wrong. You add 2 and 1 but you should add sum(range(3)) and sum(range(2)) (and without substracting 1) EDIT: correction: I forgot .values() in previous version. for n in nums.values(): good_pairs += sum(range(n)) 2 elements can create 1 pair 3 elements can create 2+1 pairs 4 elements can create 3+2+1 pairs N elements can create (N-1)+... +2+1 pairs EDIT: Instead of `sum(range(n))`` you can also use `n*(n-1)//2` for n in nums.values(): good_pairs += n*(n-1)//2
@ZipADeeeDoooDaaa
7 ай бұрын
I thought so too at first. But he is looping over nums not counter. For 1 this will sum up 2 then 1 and then 0. The alg is clever because it effectively generates all n choose 2's inside one loop at once giving a complexity of O(N)
@xingyuxiang1637
7 ай бұрын
@@ZipADeeeDoooDaaaHashMap is a tree. Why a tree is faster?
@stuartberg8030
7 ай бұрын
@@ZipADeeeDoooDaaaThank you! I had the same confusion. Our solution (iterating over the counts and summing C(C-1)/2) would be faster than his solution, though it would not change the asymptotic runtime.
@claycreate
7 ай бұрын
Netflix is not as huge as the others. Remove it from the acron- nevermind
Пікірлер: 56