XOR and the Array

Sherlock221b EWU Intra University Prog...

The XOR value of two numbers can only be 0 if the numbers are same. Therefore, all you have to do is count the number of unique element in the array. This can be done in many ways. The solution below achieves this by sorting the array in ascending order. If a number appears multiple times in the array, they will be together in the sorted array.

Since the numbers of the array can be as large as 109, you can not directly use the indices of another array for counting how many times a number appears. But in C++, you can use STL map instead of an array to achieve this.

C++ code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, ar[100005];
    int ans = 0;

    cin >> n;
    for(int i = 1; i <= n; i++) cin >> ar[i];

    sort(ar+1, ar+n+1);
    ar[0] = ar[n+1] = 0;

    for(int i = 1; i <= n; i++)
    {
        if(ar[i-1] == ar[i] || ar[i+1] == ar[i]) continue;
        ans++;
    }

    cout << ans << endl;

    return 0;
}

Statistics

84% Solution Ratio
ewuintra2016.14$6neZAEarliest, Nov '16
Paul72Fastest, 0.0s
ewuintra2016.19$C3WodLightest, 524 kB
habijabiShortest, 267B
Toph uses cookies. By continuing you agree to our Cookie Policy.