Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions
Given an array arr[] of (N – 1) integers and each value arr[i](1-based indexing) is the score of the nodes having degree i. The task is to determine the maximum score of any tree of N nodes that can be constructed.
Examples:
Input: arr[] = {1, 3, 0}
Output: 8
Explanation:
One possible way to construct tree is:
1
/ \
2 3
\
4
Node 1 have degree 2. Therefore, its score is 3.
Node 2 have degree 1. Therefore, its score is 1.
Node 3 have degree 2. Therefore, its score is 3.
Node 4 have degree 1. Therefore, its score is 1.
Therefore, the total score = 3 + 1 + 3 + 1 = 8.Input: arr[] = {0, 1}
Output: 1
Explanation:
One possible way to construct tree is:
1
/ \
2 3
Node 1 have degree 2. Therefore, its score is 1.
Node 2 have degree 1. Therefore, its score is 0.
Node 3 have degree 1. Therefore, its score is 0.
Therefore, total score = 1 + 0 + 0 = 1.
Naive Approach: The simplest approach is to generate all possible combinations of constructing a tree having N nodes and find the total score for each of them. Then, print the maximum of all the scores obtained.
Time Complexity: (N!) where N is the number of nodes in the tree.
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming by creating a dp[][] table where dp[i][j] represents the maximum score using i nodes having the sum of degrees of the nodes as j. Follow the below steps to solve the problem:
- Initialize an array dp[N + 1][2*(N – 1) + 1] where N is the number of nodes and (2*(N – 1)) is the maximum sum of degrees.
- Initialize dp[0][0] with 0.
- Iterate two nested loops, one over the range [1, N], and another for till the possible maximum score 2*(N – 1) from 1 and for each score s in the range [1, N] traverse the given array of scores arr[] and updating dp[i][s] as:
dp[i][s] = max(dp[i][s], scores[j-1] dp[i-1][s-j])
where dp[i][s] represents the maximum score of tree having i nodes and sum of degrees as s.
- For a tree with N vertices and (N – 1) edges, the sum of all degrees should be 2 * (N – 1). Therefore, print the value of dp[N][2*(N – 1)] as the maximum score for a tree with N nodes.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum score // for one possible tree having N nodes // N - 1 Edges int maxScore(vector< int >& arr) { int N = arr.size(); // Number of nodes N++; // Initialize dp[][] vector<vector< int > > dp(N + 1, vector< int >(2 * N, -100000)); // Score with 0 vertices is 0 dp[0][0] = 0; // Traverse the nodes from 1 to N for ( int i = 1; i <= N; i++) { // Find maximum scores for // each sum for ( int s = 1; s <= 2 * (N - 1); s++) { // Iterate over degree of // new node for ( int j = 1; j <= N - 1 and j <= s; j++) { // Update the current // state dp[i][s] = max(dp[i][s], arr[j - 1] + dp[i - 1][s - j]); } } } // Return maximum score for N node // tree having 2(N - 1) sum of degree return dp[N][2 * (N - 1)]; } // Driver Code int main() { // Given array of scores vector< int > arr = { 1, 3, 0 }; // Function Call cout << maxScore(arr); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the maximum score // for one possible tree having N nodes // N - 1 Edges static int maxScore( int [] arr) { int N = arr.length; // Number of nodes N++; // Initialize dp[][] int [][] dp = new int [N + 1 ][ 2 * (N - 1 ) + 1 ]; // Score with 0 vertices is 0 dp[ 0 ][ 0 ] = 0 ; // Traverse the nodes from 1 to N for ( int i = 1 ; i <= N; i++) { // Find maximum scores for // each sum for ( int s = 1 ; s <= 2 * (N - 1 ); s++) { // Iterate over degree of // new node for ( int j = 1 ; j <= N - 1 && j <= s; j++) { // Update the current // state dp[i][s] = Math.max(dp[i][s], arr[j - 1 ] + dp[i - 1 ][s - j]); } } } // Return maximum score for N node // tree having 2(N - 1) sum of degree return dp[N][ 2 * (N - 1 )] - 1 ; } // Driver Code public static void main(String[] args) { // Given array of scores int [] arr = { 1 , 3 , 0 }; // Function Call System.out.print(maxScore(arr)); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 program for the above approach # Function to find the maximum score # for one possible tree having N nodes # N - 1 Edges def maxScore(arr): N = len (arr) # Number of nodes N + = 1 # Initialize dp[][] dp = [[ - 100000 for i in range ( 2 * N)] for i in range (N + 1 )] # Score with 0 vertices is 0 dp[ 0 ][ 0 ] = 0 # Traverse the nodes from 1 to N for i in range ( 1 , N + 1 ): # Find maximum scores for # each sum for s in range ( 1 , 2 * (N - 1 ) + 1 ): # Iterate over degree of # new node j = 1 while j < = N - 1 and j < = s: # Update the current # state dp[i][s] = max (dp[i][s], arr[j - 1 ] + dp[i - 1 ][s - j]) j + = 1 # Return maximum score for N node # tree having 2(N - 1) sum of degree return dp[N][ 2 * (N - 1 )] # Driver Code if __name__ = = '__main__' : # Given array of scores arr = [ 1 , 3 , 0 ] # Function Call print (maxScore(arr)) # This code is contributed by mohit kumar 29 |
C#
// C# program for the // above approach using System; class GFG{ // Function to find the // maximum score for one // possible tree having N // nodes N - 1 Edges static int maxScore( int [] arr) { int N = arr.Length; // Number of nodes N++; // Initialize [,]dp int [,] dp = new int [N + 1, 2 * (N - 1) + 1]; // Score with 0 vertices // is 0 dp[0, 0] = 0; // Traverse the nodes from // 1 to N for ( int i = 1; i <= N; i++) { // Find maximum scores for // each sum for ( int s = 1; s <= 2 * (N - 1); s++) { // Iterate over degree of // new node for ( int j = 1; j <= N - 1 && j <= s; j++) { // Update the current // state dp[i, s] = Math.Max(dp[i, s], arr[j - 1] + dp[i - 1, s - j]); } } } // Return maximum score for // N node tree having 2(N - 1) // sum of degree return dp[N, 2 * (N - 1)] - 1; } // Driver Code public static void Main(String[] args) { // Given array of scores int [] arr = {1, 3, 0}; // Function Call Console.Write(maxScore(arr)); } } // This code is contributed by Princi Singh |
Javascript
<script> // Javascript program for the above approach // Function to find the maximum score // for one possible tree having N nodes // N - 1 Edges function maxScore(arr) { var N = arr.length; // Number of nodes N++; // Initialize dp[][] var dp = Array.from(Array(N+1), ()=> Array(2*N).fill(-10000000)); // Score with 0 vertices is 0 dp[0][0] = 0; // Traverse the nodes from 1 to N for ( var i = 1; i <= N; i++) { // Find maximum scores for // each sum for ( var s = 1; s <= 2 * (N - 1); s++) { // Iterate over degree of // new node for ( var j = 1; j <= N - 1 && j <= s; j++) { // Update the current // state dp[i][s] = Math.max(dp[i][s], arr[j - 1] + dp[i - 1][s - j]); } } } // Return maximum score for N node // tree having 2(N - 1) sum of degree return dp[N][2 * (N - 1)]; } // Driver Code // Given array of scores var arr = [1, 3, 0]; // Function Call document.write( maxScore(arr)); </script> |
8
Time Complexity: O(N3)
Auxiliary Space: O(N2)