Shirt - Matching Pairs
Shirt - Matching Pairs
PROBLEM STATEMENT :
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
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,a,cnt;
scanf("%d",&n);
int hash[1001];
for(i=0;i<n;++i)
{
scanf("%d",&a);
hash[a]++;
if(hash[a]%2==0) cnt++;
}
printf("%d",cnt);
}
C++ (CPP)
#include <iostream>
#include<map>
using namespace std;
int main(int argc, char** argv)
{
int n,a,i,cnt=0;
cin>>n;
map<int,int> hm;
for(i=0;i<n;++i)
{
cin>>a;
hm[a]++;
if(hm[a]%2==0) cnt+=1;
}
cout<<cnt;
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,a,cnt=0;
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
for(i=0;i<n;++i)
{
a=sc.nextInt();
if(!hm.containsKey(a)) hm.put(a,1);
else hm.put(a,hm.get(a)+1);
if((hm.get(a))%2==0) cnt+=1;
}
System.out.println(cnt);
}
}
PYTHON
n=int(input())
l=list(map(int,input().split()))
done=[];res=0
for i in range(n):
if l[i] not in done:
res+=(l.count(l[i]))//2
done.append(l[i])
print(res)
Comments
Post a Comment