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.
NOTE: The difference between two elements A and B will be considered as abs(A-B) or |A-B| which always be a positive number.
Example:
Input: n = 3
Output: 3
Approach
C++
#include <bits/stdc++.h>using namespace std;long long permutationAgain(long long n){long long dp[n + 1];dp[1] = 1;dp[2] = 1;for (long long i = 3; i <= n; i++){if (i & 1)dp[i] = i - 1 + dp[i - 1];elsedp[i] = i + dp[i - 1];}return dp[n];}int main(){long long n = 3;cout << permutationAgain(n) << "\n";return 0;}
No comments:
Post a Comment