WA in DIVIDING

DIVIDING - Link.
Code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    long long summation = ((n*(n+1))/2);
    long long s = 0;
    for (int i = 0; i < n; ++i)
    {
        int num;
        cin >> num;
        s += num;
    }
    if (s == summation)
        cout << "YES\n";
    else
        cout << "NO\n";
}

I got WA because of possible overflow. That’s why I assigned summation to long long. Now I’m still getting WA. What did I do wrong?

1 Like

Refer this solution

You declared num of type int when num can take values upto 10^9.
You defined n of type int and when you stored n*(n+1)/2 in summation didn’t typecast explicitly which might result in a possible overflow.

Modified your code and it runs fine now: CodeChef: Practical coding for everyone

3 Likes

Thanks. This worked. :slightly_smiling_face: