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…
ssjgz
June 4, 2021, 4:12pm
2
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
pratham_aher:
for
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