Monday, August 21, 2017

Shirt - Matching Pairs

A shop to increase sales during a festival has an offer that a customer will get a discount if the customer buys shirts having same size in pairs. Any customer who buys will choose N shirts and the size of the shirt is denoted by S(i) where 1 <= i <=N. Two shirts S(i) and S(j) are matching and form a pair only if S(i) = S(j).
The program must print the number of pairs eligible for the discount.

Input Format:
The first line will contain the value of N The second line will contain the the size of N shirts S(1) to S(N) with each size separated by a space.

Output Format:
The first line will contain the number of matching pairs eligible for the discount.

Constraints:
2 <= N <= 100

Example Input/Output 1:
Input:
9
10 20 20 10 10 30 44 10 20
Output:
3
Explanation:
The matching pairs are (10,10) (20,20) (10,10).

Example Input/Output 2:
Input:
6
42 44 40 42 44 42
Output:
2
Explanation:
The matching pairs are (42,42) (44,44)

Solution-1:
n = int(input())
a,c,s = [int(i) for i in input().split()],[0]*101,0
for i in a:
    c[i]+=1
a = set(a)
for i in a:
    if c[i]>1:
       s+=c[i]//2
print(s)

Solution-2:
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char** argv)
{
 int n,i,j=0,s=0;
 cin>>n;
 int a[n],b;
 map<int,int> c;
 for(i=0;i<n;i++)
 {
    cin>>b;
    c[b]++;
    if(c[b]==1)
     a[j++] = b;
 }
 for(i=0;i<j;i++)
   if(c[a[i]]>1)
    s+=c[a[i]]/2;
 cout<<s;
}
Share: