Discussion About Compute F(N)

This is Discussion thread about Compute F(N)

Please explain what logic will be used to solve this problem ??
My logic is getting TLE

are you mad test case is 10^18?
if we can answer in O(1) then also it is giving tle to me.

package HackerBlocks;

import java.util.Scanner;

public class Series {

public static void main(String[] args) {
	int times = 0;
	Scanner scan=new Scanner(System.in);
	if(scan.hasNextInt()) {
		times=scan.nextInt();
	}
	while(times>0) {
		long n=0;
		if(scan.hasNextInt())
			n=scan.nextInt();
		 long sum=0;
		for(int i=1;i<=n;i++) {
			if(i%2==0) {
				sum+=i;
			}else {
				sum-=i;
			}
		}
		System.out.println(sum);
		times--;
	}
}

}

this is my code. It is running fine in eclipse but it is showing error here, it would be great if someone could help

We can do it like this if the number is even then the sum=n/2 and if odd then it will be sum=-(n/2 + 1)
eg: for n=1 sum=-1;
n=2 sum=1; n=3 sum =-2 , n=4 sum=2 ,n=5 sum=-3 n=6 sum=3 …

This is clearly a recursive relation for which we can derive an O(1) formula.
f(n) = f(n-2k) + k(-1^n)
if n is even:
let k = n/2;
then f(n) = f(0) + n/2
else (when n is odd):
let k = (n-1)/2;
then f(n) = f(1) - (n-1)/2; (negative because (-1)^odd number)

Now the base cases are: f(0) = 0, f(1) = -1;
~ O(T*1)

            if(i%2==0)
			sum+=i;
			else
			sum-=i;

#include<bits/stdc++.h>
using namespace std;
int main() {
long long a,b,s,e;
cin>>a;
while(a–){
cin>>b;
s=0;
e=0;
long long f;
f=b/2;
s=((f+1)f);
if(b%2==0){
e=f
f;
}
else
e=(f+1)*(f+1);

cout<<s-e<<endl;
}
return 0;
}
i think this better than other…