0

While I was solving a question from codeforces(link to the question: https://codeforces.com/contest/1466/problem/A) I encountered weird output from the below code

#include<iostream>
#include<vector>
#include<set>

using namespace std;

void solve(){
    int64_t n;
    cin>>n;
    if (n<2){
            cout<<0<<endl;
            return;
    }
    vector<int64_t> vec(n, 0);
    for(auto &i: vec){
            cin>>i;
    }
    set<int64_t> s;
    for(int64_t i=0; i<n-1; ++i){
        for(int64_t j=i+1; j<n; ++j){
            s.insert(vec[j]-vec[i]);
        }
    }
    cout<<s.size()<<endl;
}

int main(){
    int64_t t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Input:

8
4
1 2 4 5
3
1 3 5
3
2 6 8
2
1 2
1
50
5
3 4 5 6 8
3
1 25 26
6
1 2 4 8 16 32

Ouptut given by my code:

4
2
3
1
0
53
1
1

When I removed if block it passed all the pretests/testcases.

    if (n<2){
            cout<<0<<endl;
            return;
    }

Expected output:

4
2
3
1
0
5
3
15

After removing the if block I get the same output as above. I'm not able to understand how if block is affecting the size of set in the next iteration.

Compiled with g++ same output with -std=c++11 -std=c++14 and -std=c++17

Ch3steR
  • 15,132
  • 4
  • 20
  • 45

1 Answers1

2
if (n<2){
    cout<<0<<endl;
    return;
}

Since you're returning here, it means that if n is 1, you never read that one value out of std::cin.

If you remove that if, then that one value will get read in here:

vector<int64_t> vec(n, 0);
for(auto &i: vec){
        cin>>i;
}

So if you want the if to work as expected, it should be:

if (n<2){
    cout<<0<<endl;
    if(n == 1) { std::cin >> n; } // remove 1 item from stream
    return;
}
scohe001
  • 13,879
  • 2
  • 28
  • 47
  • Ahh i'm so dumb. Thank you. I would love to blame it on time pressure in contests but this one is too dumb :( – Ch3steR Dec 30 '20 at 17:01
  • One more question can I use `cin.clear()` in the `if (n<2)` block instead of `if(n == 1) { std::cin >> n; }`? Like mentioned in this post https://stackoverflow.com/q/257091/12416453 – Ch3steR Dec 30 '20 at 17:06
  • 1
    @Ch3s `clear()` just resets the error flags on the stream, so that's almost definitely not what you want. You could do `std::cin.ignore(std::numeric_limits::max(), '\n')` to read up to the next newline (or the end of the stream if one doesn't exist). But this is only if you're SURE that the input will look like you've posted it. `std::cin >> n` actually parses for the integer and will stop at any whitespace or non-numeric characters. – scohe001 Dec 30 '20 at 17:09