Hackerblocks - Compute F(n)

https://hack.codingblocks.com/app/practice/3/p/68
LINK TO THE PROBLEM^

MY CODE:
#include
#include<math.h>
using namespace std;
int main() {
int t,n;
int sum=0;
cin>>t;
for(int i=0;i<t;++i){
cin>>n;
for(int j=1;j<=n;++j){
sum+=(pow(-1,j)*j);
}
cout<<endl<<sum;
sum=0;
}
return 0;
}

QUESTION: I faced time limit exceeded error. How can I optimise this code?

@poojagera as n and T both are going upto 10^18 so you cannot write a solution with O(n) complexity.
Try to form a series where you can directly compute the answer

1 Like