SPOJ Problem: OLOLO

Post on 05-Apr-2017

80 views 0 download

transcript

Level up your coding skills with

the C++ Standard Template

Library (STL):

SPOJ Problem: OLOLO

BY JOYJIT CHOUDHURY

OLOLO - Onotole needs your help

Is accessible at - http://www.spoj.com/problems/OLOLO/

Problem Statement

Onotole has a lot of pyani. Each pyani has a number, writing on it.

Pyanis with equal numbers are indistinguishable.

Onotole knows everything, so, he knows that each pyani appeared twice, and only one pyani is unique. He wants to get вздръжни эффект, and he needs the unique pyani.

Given the list of pyanis denote which one of them appeared once (it is guaranteed that other pyanis appeared twice).

Input First line of input contains number of pyanis

N <= 500 000. Next N lines contain a single positive

integer 1 <= Pi <= 10^9.

Output Output one positive integer on pyani, which appeared

once.

Time limit: 0.134sSource limit: 50000BMemory limit: 1536MB

So, the idea is…

There will be N numbers as input

All the numbers come in pairs, except one of them

We have to output that one number

So…

There can be many different approaches to solving this problem (read: using XOR)

A solution using std::unordered_set will be demonstrated here

For every number Pi, we check whether it’s already in the set or not:

If it’s not present, that means it’s the first occurrence of the number. Thus, we insert it into the set

If it is present, that means it has occurred once before. So, we remove it from the set

In the End

There will be just one number left in the unordered_set

The one which didn’t come in pair

The one we need to output

Code: I

Code: II

Solution Accepted!