Showing wrong answer, but I think that my solution is right

problem - CodeChef: Practical coding for everyone

my solution -
#include
using namespace std;

int main() {
int n,sum=0;
cin >> n;
for(int i=1; i <= n; i++){
sum += i;
}
cout << sum;
return 0;
}

I tried custom input which gives right answer…but on submission it says wrong ans…
what am I doing wrong ?
please help…

This Problem doesn’t allow people to view solutions (I get Accessed Denied).

Please post your (formatted!) code here!

@ssjgz thanks , for telling me that…
I have edited the post…

I guess time complexity here is O(1), so you won’t be able to use a loop

there are two mistakes. first you should use long long data type instead of int. And instead of loop, use addition formula n*(n+1)/2. Loop may give TLE

@aamodpandey thanks…that worked.

@shubh_1664 yes, that was the problem… thanks

1 Like

glad