Given an integer **N**, the task is to construct a tree such that sum of for all ordered pairs **(u, v)** is maximum where **u != v**. Print the maximum possible sum.

**Examples:**

Input:N = 4Output:26 1 / 2 / 3 / 4 For node 1, 1*2 + 1*2 + 1*1 = 5 For node 2, 2*1 + 2*2 + 2*1 = 8 For node 3, 2*1 + 2*2 + 2*1 = 8 For node 4, 1*1 + 1*2 + 1*2 = 5 Total sum = 5 + 8 + 8 + 5 = 26Input:N = 6Output:82

**Approach:** We know that sum of the degree of all nodes in a tree is **(2 * N) – 2** where **N** is the number of nodes in the tree. As we have to maximize the sum so we have to minimize the number of leaf nodes as the leaf nodes have the minimum degree among all the nodes of the tree and the tree will be of the form:

1 / 2 / ... / N

where only the root and the only leaf node will have a degree of 1 and all the other nodes will have degree 2.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to return the maximum possible sum ` `ll maxSum(` `int` `N) ` `{ ` ` ` `ll ans = 0; ` ` ` ` ` `for` `(` `int` `u = 1; u <= N; u++) { ` ` ` `for` `(` `int` `v = 1; v <= N; v++) { ` ` ` `if` `(u == v) ` ` ` `continue` `; ` ` ` ` ` `// Initialize degree for node u to 2 ` ` ` `int` `degreeU = 2; ` ` ` ` ` `// If u is the leaf node or the root node ` ` ` `if` `(u == 1 || u == N) ` ` ` `degreeU = 1; ` ` ` ` ` `// Initialize degree for node v to 2 ` ` ` `int` `degreeV = 2; ` ` ` ` ` `// If v is the leaf node or the root node ` ` ` `if` `(v == 1 || v == N) ` ` ` `degreeV = 1; ` ` ` ` ` `// Update the sum ` ` ` `ans += (degreeU * degreeV); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` `cout << maxSum(N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG ` `{ ` ` ` `// Function to return the maximum possible sum ` `static` `int` `maxSum(` `int` `N) ` `{ ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `for` `(` `int` `u = ` `1` `; u <= N; u++) ` ` ` `{ ` ` ` `for` `(` `int` `v = ` `1` `; v <= N; v++) ` ` ` `{ ` ` ` `if` `(u == v) ` ` ` `continue` `; ` ` ` ` ` `// Initialize degree for node u to 2 ` ` ` `int` `degreeU = ` `2` `; ` ` ` ` ` `// If u is the leaf node or the root node ` ` ` `if` `(u == ` `1` `|| u == N) ` ` ` `degreeU = ` `1` `; ` ` ` ` ` `// Initialize degree for node v to 2 ` ` ` `int` `degreeV = ` `2` `; ` ` ` ` ` `// If v is the leaf node or the root node ` ` ` `if` `(v == ` `1` `|| v == N) ` ` ` `degreeV = ` `1` `; ` ` ` ` ` `// Update the sum ` ` ` `ans += (degreeU * degreeV); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `6` `; ` ` ` `System.out.println(maxSum(N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function to return the maximum possible sum ` `def` `maxSum(N) : ` ` ` `ans ` `=` `0` `; ` ` ` ` ` `for` `u ` `in` `range` `(` `1` `, N ` `+` `1` `) : ` ` ` `for` `v ` `in` `range` `(` `1` `, N ` `+` `1` `) : ` ` ` `if` `(u ` `=` `=` `v) : ` ` ` `continue` `; ` ` ` ` ` `# Initialize degree for node u to 2 ` ` ` `degreeU ` `=` `2` `; ` ` ` ` ` `# If u is the leaf node or the root node ` ` ` `if` `(u ` `=` `=` `1` `or` `u ` `=` `=` `N) : ` ` ` `degreeU ` `=` `1` `; ` ` ` ` ` `# Initialize degree for node v to 2 ` ` ` `degreeV ` `=` `2` `; ` ` ` ` ` `# If v is the leaf node or the root node ` ` ` `if` `(v ` `=` `=` `1` `or` `v ` `=` `=` `N) : ` ` ` `degreeV ` `=` `1` `; ` ` ` ` ` `# Update the sum ` ` ` `ans ` `+` `=` `(degreeU ` `*` `degreeV); ` ` ` ` ` `return` `ans; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `6` `; ` ` ` `print` `(maxSum(N)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to return the maximum possible sum ` `static` `int` `maxSum(` `int` `N) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `for` `(` `int` `u = 1; u <= N; u++) ` ` ` `{ ` ` ` `for` `(` `int` `v = 1; v <= N; v++) ` ` ` `{ ` ` ` `if` `(u == v) ` ` ` `continue` `; ` ` ` ` ` `// Initialize degree for node u to 2 ` ` ` `int` `degreeU = 2; ` ` ` ` ` `// If u is the leaf node or the root node ` ` ` `if` `(u == 1 || u == N) ` ` ` `degreeU = 1; ` ` ` ` ` `// Initialize degree for node v to 2 ` ` ` `int` `degreeV = 2; ` ` ` ` ` `// If v is the leaf node or the root node ` ` ` `if` `(v == 1 || v == N) ` ` ` `degreeV = 1; ` ` ` ` ` `// Update the sum ` ` ` `ans += (degreeU * degreeV); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `N = 6; ` ` ` `Console.WriteLine(maxSum(N)); ` `} ` `} ` ` ` `// This code is contributed by Chandan_jnu ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to return the maximum ` `// possible sum ` `function` `maxSum(` `$N` `) ` `{ ` ` ` `$ans` `= 0; ` ` ` ` ` `for` `(` `$u` `= 1; ` `$u` `<= ` `$N` `; ` `$u` `++) ` ` ` `{ ` ` ` `for` `(` `$v` `= 1; ` `$v` `<= ` `$N` `; ` `$v` `++) ` ` ` `{ ` ` ` `if` `(` `$u` `== ` `$v` `) ` ` ` `continue` `; ` ` ` ` ` `// Initialize degree for node u to 2 ` ` ` `$degreeU` `= 2; ` ` ` ` ` `// If u is the leaf node or the ` ` ` `// root node ` ` ` `if` `(` `$u` `== 1 || ` `$u` `== ` `$N` `) ` ` ` `$degreeU` `= 1; ` ` ` ` ` `// Initialize degree for node v to 2 ` ` ` `$degreeV` `= 2; ` ` ` ` ` `// If v is the leaf node or the ` ` ` `// root node ` ` ` `if` `(` `$v` `== 1 || ` `$v` `== ` `$N` `) ` ` ` `$degreeV` `= 1; ` ` ` ` ` `// Update the sum ` ` ` `$ans` `+= (` `$degreeU` `* ` `$degreeV` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$N` `= 6; ` `echo` `maxSum(` `$N` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

82

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of trees whose sum of degrees of all the vertices is L
- Find the remaining vertices of a square from two given vertices
- Maximize sum of pairwise products generated from the given Arrays
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Sum of the products of same placed digits of two numbers
- Maximum Sum of Products of Two Arrays
- Sum of all products of the Binomial Coefficients of two numbers up to K
- Maximum absolute difference between any two level sum in a Binary Tree
- Maximum absolute difference between any two level sum in a N-ary Tree
- Maximize partitions such that no two substrings have any common character
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Maximize minimum distance between repetitions from any permutation of the given Array
- Sum of pairwise products
- Minimize the Sum of all the subarrays made up of the products of same-indexed elements
- Sum of all ordered pair-products from a given array
- XOR of path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Check if max sum level of Binary tree divides tree into two equal sum halves

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.