Number of Good Pairs (LeetCode 1512) | Full solution with visuals and examples | Study Algorithms
Given an array, we need to find the number of good pairs that can be formed with the elements. A pair is said to be good if the elements are same and the left index is smaller than the right index. Watch the video to see a visual example of how the pairs are determined. The video also explores a progressive way to arrive at an optimal solution along with a dry-run of the code. Simple maths can be wonderful at times.
Chapters:
00:00 - Intro
01:11 - Problem statement and description
04:54 - Brute Force approach to find good pairs
07:09 - Efficient method to find good pairs
10:52 - Dry-run of Code
12:53 - Final Thoughts
Actual problem on LeetCode: leetcode.com/problems/number-...
📚 Links to topics I talk about in the video:
Brute Force Method: • Brute Force algorithms...
Counting Sort: • Counting Sort explaine...
Bucket Sort: • Bucket Sort explained ...
What is Big O?: • Big O Notation Simplif...
Other LeetCode questions: • Leetcode Solutions
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
Пікірлер: 48
thank u for the explanation for how to think the optimise solution
Thanks for explaining in detail
man, that's thinking out of the box, great! I solved the problem with the brute force approach but I did realize "bro, this looks bad, its a O(n²) algorithm, thats no good for time" but I couldnt think of a way to solve it with O(n), thank you!
@nikoo28
9 ай бұрын
Good stuff!
Excellent explantion. Thanks
excellent explanation
Hii bruh, could you give me some efficient tips to solve problems using data structures and how can i choose efficient algorithm for solving particular probs?
Can you Explain AND xor OR problem on hackerrank in stack data structures part
nice explanation
tq sir i understood very will with u r explation
@bindhumadavalibindhu6057
Жыл бұрын
tq
amazingggggg amazingggg
Awesome
i dont understand why there is so much less views on this you explain in such a perfect manner... i was not able to understand que but i know its optimised way..to solve
@nikoo28
2 жыл бұрын
I am glad you found this helpful
@NiteenChougula-fs9ij
11 ай бұрын
Same thought.
@Homelander_30
Ай бұрын
He is goat.
If the limitation is size of input array is 100 why we took 102 size of count array?
Amazing video 😀
@nikoo28
9 ай бұрын
Thanks for the visit
Can someone confirm why the size of count array has been taken as 102 ?
How will the space complexity be 0(1) while you are creating addition freq array of size biggest number in the original array?
@nikoo28
Жыл бұрын
That is because the biggest number can only be 100. Always read the problem limits. So that means a constant space of 100 no matter the input. Hope this helps
Can anyone expalin why time complexicity is O(n) instead of O(2n) we are using 2 for loops ?
@nikoo28
11 ай бұрын
O(2n) is basically the same as O(n). Basically for small values of k O(k * n) ~ O(n)
A quick question how is efficient approach has O(n) time complexity?
@nikoo28
Ай бұрын
What should it be according to you?
How is it order of (N) since we have to populate the hashmap?
@nikoo28
2 жыл бұрын
Populating the hashmap will just require one iteration of the array. Hence it keeps the time complexity O(n).
Going ahead with your optimised solution , I've removed that nC2 calculation part and I'm adding count[num]++ to totalCount in the first for loop only. class Solution { public int numIdenticalPairs(int[] nums) { int[] count = new int[101]; int totalCount = 0; for(int num : nums) { totalCount+=count[num]++; //using the fact that count[num]++ is updated after assignment } return totalCount; }
@nikoo28
Жыл бұрын
love this approach
@Dadycalling55
Жыл бұрын
@@nikoo28 thank you, you're doing great work, keepItUp
@gopikaarulprakasam8195
6 ай бұрын
I can't understand this can you explain plz
I tried to find a pattern in which the count is appearing, and this is how I did my code. I knew about the permutation&combination method,but I couldn't remember the formula. int numIdenticalPairs(vector& nums) { unordered_mapmp; int n = nums.size(); int count =0; for(int i=0;i1){ count = count + mp[nums[i]] -1; } } return count; }
//cpp class NumberOfGoodPairs { public: int numIdenticalPairs(vector& nums) { // Calculate the frequency of each number vector count(102, 0); for (int num : nums) { count[num]++; } int totalCount = 0; // Calculate total number of pairs possible for (int i : count) { totalCount += (i * (i - 1)) / 2; } return totalCount; } };
What means of [102] in new int[102]
@nikoo28
2 жыл бұрын
That is the count array, storing the frequency/count of each integer
i*(i-1)/2 ..... this formula, i can't understand.... how it is same with combination formula
@nikoo28
Жыл бұрын
Try substituting values for a formula (i C 2) You will get the equivalent equation
@elyhahami4294
Жыл бұрын
n choose 2 = (n)!/((2!)(n-2)!) by definition -> n(n-1)/2 because n!=n(n-1)(n-2)! Which divides to 1 in the numerator and denominator
Love You Believe Me You are Doing Great. Please Help me to Understand AVL Tree. I am unable to understand how to maintain the height of it. I spend 30 days to do that but still unable to understand that.
@nikoo28
11 ай бұрын
to be very honest, if you are facing problems with AVL trees, you can leave them. They are hardly ever asked in coding interviews, and they do not have real life implementations as well. I don't yet have a video for AVL tree in the pipeline...but will try if needed.
@arslanmuhammad4190
11 ай бұрын
@@nikoo28 Thanks Sir, I have learn that 2 Days ago. And they are difficult to manage and maintain. But I was just want to Increase my logical thinking. So, Thanks for your help. Now, I will spend my time on AVL trees. Will u tell me which tree or trees are preferable to learn.
@arslanmuhammad4190
11 ай бұрын
@@nikoo28 But sir, BST is waste if it is not a AVL.
@arslanmuhammad4190
11 ай бұрын
@@nikoo28 To be honest You are great a teacher. Universities did not teach us coding. But You Thanks.
@arslanmuhammad4190
11 ай бұрын
Sir, You taught us Arrays, Linked List, Trees, Strings, DP Thanks. Sir, I have a request will you teach us graphs too.
A more optimized logic could be // Coding in Javascript function numIdenticalPairs(nums) { let count = 0 , freq = {} for(let i=0;i