Sales by Match | Sock Merchant HackerRank Solution [O(N)] sock merchant hackerrank problem

Sales by Match | Sock Merchant HackerRank Solution [O(N)] sock merchant hackerrank problem


Sales by Match | Sock Merchant HackerRank Solution [O(N)] sock merchant hackerrank problem

In this video, I have explained sales by match hackerrank solution algorithm.
hackerrank sock merchant problem can be solved by using set data structure. The complexity of sock merchant hackerrank solution is O (n). This hackerrank problem is a part of Practice | Algorithms | Implementation | Sock merchant hackerrank challenge.

For simplicity, I have divided this hackerrank tutorial into 3 parts.
[00:00] Understanding the problem statement.
[02:02] Building the logic to solve the problem.
[05:40] Coding the logic using java (you can use your own preferred programming language).

đź”´ DONT CLICK THIS: https://bit.ly/2G4cEuc

👍 Like us on Facebook: https://www.facebook.com/HackerRankSo…

💎Share this video with a YouTuber friend:    • Sales by Match | Sock Merchant Hacker…  

âśš Join our community â–ş

👉 Coding interview preparation group: https://www.facebook.com/groups/codingip
👉 Telegram link: https://t.me/hackerranksolutions


đź“– Resources â–ş

🙋 Problem statement: https://www.hackerrank.com/challenges…
💡 Source code: https://github.com/Java-aid/Hackerran…

âś… Recommended playlists â–ş

👉 All hackerrank solutions:    • How To Master In Data Structures And …  

👋 Let’s Connect ►

Git Hub: https://github.com/kanahaiya
Twitter: https://twitter.com/Kanahaiyagupta
Linked in: https://www.linkedin.com/in/kanahaiya…
Facebook: https://www.facebook.com/coolkanahaiya
Instagram: https://www.instagram.com/coolkanahaiya


#JAVAAID #salesbymatch #HackerRankSolutions #HackerRankTutorials #HackerRank #JavaAidTutorials #programming #DataStructures #algorithms #coding #competitiveprogramming #java #codinginterview #problemsolving #KanahaiyaGupta #hackerrankchallenges #Implementation


Content

0.77 -> Hello everyone welcomes to my YouTube channel. today we are going to talk about sock merchant
7.5 -> hackerrank problem.In this problem, we have given an array of integers representing the
16.129 -> color of each sock and we have to determine how many pairs of socks with matching colors
23.72 -> there are.We will first look at the input format, the first line contains an integer
31.949 -> n, the number of socks represented in the array. the second line contains n space separated
39.87 -> integers, describing the color array of the socks in the pile. Let's try to look at the
47.68 -> example first. this line represents the number of elements present in the array which is
53.969 -> 9 over here and these are the nine elements. guys here each number represents one color
63.75 -> of this socks pair and our task is to find out how many socks pairs are available. so
74.33 -> here if you can see the first element is 10 and we have one more 10 here so this one
83.819 -> pair. 20 & 20 this the second pair. and we have 10 and 10 one more pair. so totally
97.049 -> have 1 + 1 + 1 equal to 3 pairs this is the answer .so our task is to if given an array
111.119 -> of n elements and we have to find out how many socks pairs are present. so let's try
119.649 -> to understand the algorithm behind this here I am taking the same example guys which we
127.329 -> have seen in a sample. first here we will take one set which is empty set and a variable
137.95 -> or counter which will maintain the pair count C. initially it will be zero so you can see
147.79 -> this the count and we will process the element one by one in a set so whenever we found anything
161.28 -> in the set it means we got the pair and we will remove that pair, that element from the
170.61 -> set and increment the counter. let's see how we are going to do it. first we will process
178.101 -> the 10 so the counter will be zero so we will process the next element 20. 20 is not present
186.89 -> in the set so we will add the 20 here so counter still will be zero. We will process the next
196.12 -> element which is again 20 so if you can see guys 20 is already present it means we found
201.74 -> the matching pair. so we'll just remove this element from the set and increment the counter
212.06 -> because we got one pair of socks. now will process the next element which is 10 as we
220.6 -> can see 10 is already there so it means we got one more pair of socks. so will remove
228.24 -> this element from the set and increment the counter to 2. Now will process the next element
236.83 -> which is again 10 so, because it was empty there was nothing so we will just add into
244.3 -> the set and still the counter will be 2. Now will process the next element 30 and will
253.77 -> check whether 30 is present in the set or not because 30 is not there so we will just
258.65 -> add that element into the set and the counter still remains same. will process the next
267.02 -> element which is 50 and we will add into the set because there is no 50 over here now,
276.259 -> we will process again the 10 and we can see 10 is already there. so will remove the 10
283.52 -> from the set because we got another pair of 10. so only 30 and 50 will be here and will
291.789 -> increment the counter by 1. because we got one pair of 10. now we will process the next
300.629 -> element which is 20. So here you can see, we will have 20 over here because 20 is not
309.259 -> present here and the counter will remain the same.Once we have done with the processing
316.169 -> will come to know that set will be containing these three elements and the counter will
321.49 -> be 3 which will tell this is the final result. and these many numbers of pairs are present
330.599 -> in the array. so, this is the logic guys, now will see the solution. so, this is the method,
344.59 -> guys. in the method we have n which represent the number of elements present in the array
349.79 -> and an input array.so we have taken a HashSet over here and this the counter which will
359.659 -> calculate the sock pairs count and we are going through a loop and we are looping from
368.49 -> 0 to n -1 and we are taking each element from the array and checking whether set contains
378.319 -> that element or not.If set contains that element we are removing that element from the set
387.49 -> and incrementing the count as we did in our algorithm and if set does not contain that
394.319 -> element we are adding that element into the set.so once we have done with the loop the
402.18 -> counter or the count variable will hold the exact matching pair of socks and we will return that
409.96 -> Let's try to run this code on hackerrank platform so, in this, I am just copy-pasting
426.3 -> the code here and try to run.
436.82 -> so guys you can see sample test case got passed.
442.9 -> I will try to submit my code.
451.46 -> congratulations guys. it is accepted by hackerrank platform so I
457.33 -> hope you got the idea how we are going to solve this question. guys I have committed
464.59 -> the code on GitHub and link is mentioned in the description box.so you can download the
471.089 -> code from there.
474.04 -> If you think this video helped you to gain some knowledge then please subscribe
478.529 -> to my YouTube channel guys.thanks for watching.

Source: https://www.youtube.com/watch?v=NPMxxSyE7jM