Monday, 31 December 2012

Onotole needs your help


SPOJ Problem Set (classical)

7742. Onotole needs your help

Problem code: OLOLO

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.

Example

Input:
3
1
8
1
Output:
8



SOLUTION--

ALL ELEMENTS IN THE ARRAY APPEAR TWICE IN THE LIST EXCEPT ONE.

SO THERE IS A EASY AND BEST SOLUTION WITH O(n) TIME AND O(1) SPACE.

USING X-ORing OF ALL THE ELEMENTS THEN THE FINAL ANSWER WILL SHOW THE UNIQUE ELEMENTS IN THE GIVEN LIST .

SOURCE CODE---


#include
int main()
{
    int n,i;
    scanf("%d",&n);
    long long int arr[n];
    for(i=0;i<n;i++)
        scanf("%lld",&arr[i]);
    long long int a,ans;
    a=arr[0];
    for(i=1;i<n;i++)
    {
        ans=a^arr[i];
        a=ans;
    }
    printf("%lld\n",ans);
    return 0;
}


/* HERE a is a variable having value at position zero and the then there is a variable which is equal to X-OR of element a and arr[i] and then we assign value of ans in variable a. 


if you find any bugs or more efficient solution than this than post in comments.
thnxx!!

5 comments:

Unknown said...

This does the same thing without the loops :

int main ()
{
int cases,num,ans=0;
scanf("%d",&cases);
while (cases--)
{
scanf("%d",&num);
ans = ans^num;
}
printf("%d\n",ans);
return 0;
}

Unknown said...

This does the same thing without the loops :

int main ()
{
int cases,num,ans=0;
scanf("%d",&cases);
while (cases--)
{
scanf("%d",&num);
ans = ans^num;
}
printf("%d\n",ans);
return 0;
}

CHANDAN SINGH said...

i did the same but i used a extra loop for reading the numbers. Anyway nice solution thank you !! :)
Regards
Chandan Singh

CHANDAN SINGH said...

i did the same but i used a extra loop for reading the numbers. Anyway nice solution thank you !! :)
Regards
Chandan Singh

CHANDAN SINGH said...

i did the same but i used a extra loop for reading the numbers. Anyway nice solution thank you !! :)
Regards
Chandan Singh