Sock Merchant Hackerrank Problem coderinme

Sock Merchant

John’s clothing store has a pile of n loose socks where each sock i is labeled with an integer, ci, denoting its color. He wants to sell as many socks as possible, but his customers will only buy them in matching pairs. Two socks, ci and xj, are a single matching pair if ci=cj.

Given n and the color of each sock, how many pairs of socks can John sell?

Input Format

The first line contains an integer,n , denoting the number of socks.
The second line contains n space-separated integers describing the respective values of c1,c2,… cn-1.

Constraints
1<=n<=100 1<=ci<=100 Output Format Print the total number of matching pairs of socks that John can sell. Sample Input 9 10 20 20 10 10 30 50 10 20 Sample Output 3 Sock Merchant

I Method
#include <bits/stdc++.h>
using namespace std;


int main() {
	int n;
    cin>>n;
	int freq[101] = {};
	for(int i = 0; i < n; i++) {
        int c;
        cin >> c;
        freq[c]++;
    }

	int res = 0;
	for(int i = 0; i <= 100; i++){
         res += freq[i] / 2;
     }
	cout << res << endl;
	return 0;
}
II Method
#!/bin/python3

import sys

from collections import Counter
input()
socks, pairs = Counter(map(int,input().strip().split())), 0
for s in socks: pairs += socks[s]//2
print(pairs)

Sock Merchant

Competitive coding
Hackerrank problem

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.