HackerRank 'Sherlock and Pairs' Solution

by Srikant Padala on June 2, 2016, 11:38 am


Explanation


  • Sort the array.
  • Count each repeating elements say C.
  • Find CP2 and add it to the sum (Note that CP2 = C * (C-1) may cross maximum integer value, hence typecast one number to long long).

Sherlock and Pairs Problem Statement

Video

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long long getPairs(vector<int> &a) {
    long long sum = 0;
    sort(a.begin(), a.end());
    for(int i=1; i < a.size(); i++) {
        int c = 1;
        while(i < a.size() && a[i] == a[i-1]) {
            c++;
            i++;
        }
        sum += ((long long)c * (c-1)); //cP2
    }
    return sum;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t, n;
    cin >> t;
    while(t--) {
        cin >> n;
        vector<int> a(n);
        for(int i=0; i < n; i++) cin >> a[i];
        cout << getPairs(a) << endl;
    }
    return 0;
}

Coming Soon.