ArenaRating

solved.ac Grand Arena #1

Matching Socks

Solution 1

If there's a number written odd number of times, it's impossible to pair that number entirely, inevitably leaving one left.

Code (C++)

#include <iostream> #include <vector> using namespace std; int main() { vector<int> cnt(10); for (int i = 0; i < 5; i++) { int Ai; cin >> Ai; cnt[Ai]++; } for (int i = 0; i < 10; i++) if (cnt[i] % 2 == 1) cout << i << endl; }

Code (Python)

def main(): cnt = [0] * 10 for i in range(5): Ai = int(input()) cnt[Ai] += 1 for i in range(10): if cnt[i] % 2 == 1: print(i) if __name__ == '__main__': main()

Solution 2

The properties of the XOR (exclusive OR) operation \oplus are as follows:

Due to the properties of XOR, if you XOR the same number twice, the number will disappear. Thus, to find the only number that can't be paired, XOR all the numbers given in the input.

Code (C++)

#include <iostream> using namespace std; int main() { int ans = 0; for (int i = 0; i < 5; i++) { int Ai; cin >> Ai; ans ^= Ai; } cout << ans << endl; }

Code (Python)

def main(): ans = 0 for i in range(5): Ai = int(input()) ans ^= Ai print(ans) if __name__ == '__main__': main()