Solution of 'Sales by Match' (Hackerrank)

Hi all!

Here's my solution to the problem 'Sales by Match' from HackerRank.

Question link: https://www.hackerrank.com/challenges/sock-merchant/problem

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;


int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,ans=0;
    cin>>n;
    vector<int> arr;
    vector<int>c;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        arr.pb(x);
    }
    sort(arr.begin(),arr.end());
    for(int i=0;i<n;i++){
        int a,cnt=0;
        a=arr[i];
        while(a==arr[i]){
            ++cnt;
            i++;
        }
        c.pb(cnt);
        i--;
    }
    for(int i=0;i<c.size();i++){
        c[i]=c[i]/2;
        ans+=c[i];
    }
    cout<<ans;
    return 0;
}

Comments

Popular posts from this blog

Solution of 'The Two Dishes' (Codechef)

Solution of 'Tom Riddle's Diary' (Codeforces)

Solution of 'Less or Equal' (Codeforces)