Why this constraint error?

I was solving this problem Counting Inversions Revisited Problem Code: INVYCNT. Here it is clearly mentioned that

n <= 100 and a[i] <= 10^ 9

I tried to solve this problem using int array but I got WA.



WA code link

When I changed the array type to long long I got AC


AC code link

Why is this happening? int type array can store array elements of size 10^9, then why is this happening? Can anyone give some insight into this?

Please either format your code or (better!) link to your submission - no one wants to squint at a PNG of code :slight_smile:

1 Like

That’s actually because you changed k to long long int.

Sorry, Links are updated now.

1 Like

k<=10^6. Then changing it into long long doesn’t help.

When you do ans += k*k+1 the k is still an int, so it overflows and then you add it.
@ssjgz can probably explain it more concisely.

1 Like

Got it, you mean it’s like (10^5 * 10^5+1 )/2. It’s first multiplying and then dividing.

1 Like

Ya, that. You can write ll(k) or (ll)k to make it not overflow also.

2 Likes