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

  • @devanshsrivastava4000
    @devanshsrivastava40002 жыл бұрын

    thank u for the explanation for how to think the optimise solution

  • @sulemankhan523
    @sulemankhan523 Жыл бұрын

    Thanks for explaining in detail

  • @TiagoDiass2
    @TiagoDiass29 ай бұрын

    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

    @nikoo28

    9 ай бұрын

    Good stuff!

  • @shakil-the-coding-monster
    @shakil-the-coding-monster7 ай бұрын

    Excellent explantion. Thanks

  • @shubham_01-w7b
    @shubham_01-w7b Жыл бұрын

    excellent explanation

  • @Webeverse.
    @Webeverse.2 жыл бұрын

    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?

  • @piyushpatidar1777
    @piyushpatidar17772 жыл бұрын

    Can you Explain AND xor OR problem on hackerrank in stack data structures part

  • @EgorChebotarev
    @EgorChebotarevАй бұрын

    nice explanation

  • @bindhumadavalibindhu6057
    @bindhumadavalibindhu6057 Жыл бұрын

    tq sir i understood very will with u r explation

  • @bindhumadavalibindhu6057

    @bindhumadavalibindhu6057

    Жыл бұрын

    tq

  • @AyushSharma-ud8hc
    @AyushSharma-ud8hc Жыл бұрын

    amazingggggg amazingggg

  • @BECSE-SSubiramaniyan
    @BECSE-SSubiramaniyan2 жыл бұрын

    Awesome

  • @sajiyasalat
    @sajiyasalat2 жыл бұрын

    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

    @nikoo28

    2 жыл бұрын

    I am glad you found this helpful

  • @NiteenChougula-fs9ij

    @NiteenChougula-fs9ij

    11 ай бұрын

    Same thought.

  • @Homelander_30

    @Homelander_30

    Ай бұрын

    He is goat.

  • @mahimatolani813
    @mahimatolani813 Жыл бұрын

    If the limitation is size of input array is 100 why we took 102 size of count array?

  • @11csepratikshaargulewar71
    @11csepratikshaargulewar719 ай бұрын

    Amazing video 😀

  • @nikoo28

    @nikoo28

    9 ай бұрын

    Thanks for the visit

  • @shubhamsaxena6349
    @shubhamsaxena63495 ай бұрын

    Can someone confirm why the size of count array has been taken as 102 ?

  • @anshulkupadhyay
    @anshulkupadhyay Жыл бұрын

    How will the space complexity be 0(1) while you are creating addition freq array of size biggest number in the original array?

  • @nikoo28

    @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

  • @me-ooh-no
    @me-ooh-no11 ай бұрын

    Can anyone expalin why time complexicity is O(n) instead of O(2n) we are using 2 for loops ?

  • @nikoo28

    @nikoo28

    11 ай бұрын

    O(2n) is basically the same as O(n). Basically for small values of k O(k * n) ~ O(n)

  • @n1x404
    @n1x4043 ай бұрын

    A quick question how is efficient approach has O(n) time complexity?

  • @nikoo28

    @nikoo28

    Ай бұрын

    What should it be according to you?

  • @solar679
    @solar6792 жыл бұрын

    How is it order of (N) since we have to populate the hashmap?

  • @nikoo28

    @nikoo28

    2 жыл бұрын

    Populating the hashmap will just require one iteration of the array. Hence it keeps the time complexity O(n).

  • @Dadycalling55
    @Dadycalling55 Жыл бұрын

    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

    @nikoo28

    Жыл бұрын

    love this approach

  • @Dadycalling55

    @Dadycalling55

    Жыл бұрын

    @@nikoo28 thank you, you're doing great work, keepItUp

  • @gopikaarulprakasam8195

    @gopikaarulprakasam8195

    6 ай бұрын

    I can't understand this can you explain plz

  • @crosswalker45
    @crosswalker459 ай бұрын

    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; }

  • @SubhajitDas-mt7sn
    @SubhajitDas-mt7snАй бұрын

    //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; } };

  • @vikky01
    @vikky012 жыл бұрын

    What means of [102] in new int[102]

  • @nikoo28

    @nikoo28

    2 жыл бұрын

    That is the count array, storing the frequency/count of each integer

  • @gokulakannan3664
    @gokulakannan3664 Жыл бұрын

    i*(i-1)/2 ..... this formula, i can't understand.... how it is same with combination formula

  • @nikoo28

    @nikoo28

    Жыл бұрын

    Try substituting values for a formula (i C 2) You will get the equivalent equation

  • @elyhahami4294

    @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

  • @arslanmuhammad4190
    @arslanmuhammad419011 ай бұрын

    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

    @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

    @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

    @arslanmuhammad4190

    11 ай бұрын

    @@nikoo28 But sir, BST is waste if it is not a AVL.

  • @arslanmuhammad4190

    @arslanmuhammad4190

    11 ай бұрын

    @@nikoo28 To be honest You are great a teacher. Universities did not teach us coding. But You Thanks.

  • @arslanmuhammad4190

    @arslanmuhammad4190

    11 ай бұрын

    Sir, You taught us Arrays, Linked List, Trees, Strings, DP Thanks. Sir, I have a request will you teach us graphs too.

  • @the_story_mode
    @the_story_mode Жыл бұрын

    A more optimized logic could be // Coding in Javascript function numIdenticalPairs(nums) { let count = 0 , freq = {} for(let i=0;i

Келесі