Permutation Again

QUESTION

Given an integer N. Find out the PermutationSum where PermutationSum for integer N is defined as the maximum sum of difference of adjacent elements in all arrangement of numbers from 1 to N.\n\nNOTE: Difference between two elements A and B will be considered as abs(A-B) or |A-B| which always be a positive number.\n\nInput:\nFirst line of input contains number of test case T. Each test case contains a single integer N.\n\nOutput:\nFor each test case print the maximum value of PermutationSum.\n\nConstraints:\n1<=T<=10000\n1<=N<=10^5.

ANSWER

#include <stdio.h>
 
int main()
{
    int t;
    long n;
    long long ans;
    scanf("%d",&t);
    while(t--){
    	scanf("%ld",&n);
    	ans=1;
    	if(n!=1)
    	ans=n*(n-1)/2;
    	if(n%2==0)
    	ans+=(n-1)/2;
    	else
    	ans+=(n-2)/2;
    	printf("%lld\n",ans);
    }
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.