CIN005-Editorial

PROBLEM LINK:

https://www.codechef.com/NITJ2021/problems/CIN005

Author: Pawan Kholiya(CodeChef User | CodeChef)
Tester: Pawan Kholiya(CodeChef User | CodeChef)
Editorialist: Pawan Kholiya(CodeChef User | CodeChef)

DIFFICULTY:

MEDIUM

PREREQUISITES:

HashMap

PROBLEM:

Statement:

Chef is making Window frames for his new office, for this he has n wooden Logs whose lengths are l1, l2, … ln respectively. Chef Doesn’t want to break any logs or Stick 2 or more logs together.

To make a h × w Window Frame, he needs two Logs with lengths equal h and two with length .

The Chef wants as much sunlight in as possible and for it he has decided to make from the available logs as many frames as possible. Help him in finding the number of window Frames that he can make.

Note : Chef do not need to use all the logs

Input:

  • The first line of the input contains a single integer T denoting the number of test cases. The description of each test case follows :.
  • The first line of each test case contains a single integer n the number of wooden logs.
  • The second line contains n space-separated integers l1,l2,l3….ln The length of each wooden log

Output:

  • The only line in Output Contains single Integer denoting the maximum possible number of Wooden Frames.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ n ≤ 100
  • 1 ≤ li ≤ 10000

Example Input:

2

4

1 2 1 2

8

1 2 1 3 4 1 5 6

Example Output:

1

0

Explanation :

First Case : We can build a frame of dimension 1x2 as two logs of each dimension are available.

Second Case : We can’t build any Frame as no logs of length except 1 have more than one piece.

QUICK EXPLANATION:

use a hashmap to count the number of logs of length i and then summate their halves to get the number of couples.

EXPLANATION:

Create an array hash[10000] in which i-th element of it stores the number of logs with lengths equal to i.
Now you should calculate the number of couples of logs of equal lengths.
This number can be calculated as :
couples = Σ[ hash[i]/2 ]
,where [f] is the floor of f.

For every window frame the chef will need 2 if such couples,
Therefore the answer will be
count = [ couples/ 2].

SOLUTIONS:

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
ll t;
cin>>t;
while(t–)
{
ll n;
cin>>n;
ll a[n+5],i,j,k;
for(i=0;i<n;i++)
{
cin>>a[i];
}
ll hash[10005] ={0};

            for(i=0;i<n;i++)
            {
                    hash[a[i]]++;
            }
            ll couples = 0, count;

            for(i=0;i<10005;i++)
            {
                    couples+= hash[i]/2;
            }
            count=couples/2;
            cout<<count<<endl;
    }

}