Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i stuck at a problem SPOJ.com - Problem OLOLO[^]

I am taking XOR of all the value finally all values that are coming two times will be canceled and remaining value will be ans

1) I used
cin>>temp
with
std::ios::sync_with_stdio(false);

in while loop getting "WA" (I used above line Using scanf() in C++ programs is faster than using cin? - Stack Overflow[^] )
And time complexity was tight

2)only used cin>>temp got TLE

3)used only
scanf("%d",&temp)
accepted

What I have tried:

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long int t;
    cin>>t;
    std::ios::sync_with_stdio(false);
    long long int ans=0;
    while(t--){
        long long int temp;
        cin>>temp;
        ans=ans^temp;

    }
    cout<<ans;
}

can someone explain why my answer not get accepted in first case according to post I did the same
Posted
Updated 14-Jan-19 9:24am
v3
Comments
Richard MacCutchan 14-Jan-19 6:45am    
You need to ask the people at SPOJ.com.

one of the reason could be a negative input which will cause while loop endlessly.
 
Share this answer
 
Comments
Patrice T 14-Jan-19 15:03pm    
Since it is a contest, I think the input is the same in both cases.
Quote:
Cin vs scanf in cpp (more faster)

Answer to this question depends on implantation details of site compiler which we don't have. Simply, it is common to see c++ code slower than c.

If you want your code faster, you need to understand how time is spent. Your code spend a lot of time converting a string of chars input to integer. When you know how time is spend, the next question is 'is there another path?'.
Do you really need that conversion ?
In fact, no.
Think how to handle input if it was names. In C/C++, a char is a 8 bits integer, so
'A'^'B'^'A' is allowed and works the same as with an integer.
I let you handle details as an exercise.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900