BackhardGraphsAppleGoldman Sachs

Quantum Network Stream Evaluator 4 Solution

Problem Statement

Given a tree with nodes and edges, find the centroid of the tree using Heavy-Light Decomposition algorithm.

Example 1
Input
Given a tree with nodes [1, 2, 3, 4, 5] and edges [(0, 1), (0, 2), (1, 3), (1, 4)], find the centroid of the tree using Heavy-Light Decomposition algorithm.
Output
The centroid of the tree is node 1 with a sum of subtree as 1 + 2 + 3 + 4 = 10

Explanation: Step-by-step: First, we find the centroid of the tree by calculating the sum of each subtree. Then, we apply the Heavy-Light Decomposition algorithm to find the node with the maximum sum of its subtree.

Example 2
Input
Given a tree with nodes [10, 20, 30, 40, 50] and edges [(0, 1), (0, 2), (1, 3), (1, 4)], find the centroid of the tree using Heavy-Light Decomposition algorithm.
Output
The centroid of the tree is node 1 with a sum of subtree as 10 + 20 + 30 + 40 = 100

Explanation: Step-by-step: First, we find the centroid of the tree by calculating the sum of each subtree. Then, we apply the Heavy-Light Decomposition algorithm to find the node with the maximum sum of its subtree.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N log N) or O(N log^2 N)
  • Space Complexity: O(N)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Quantum Network Stream Evaluator 4 — Problem Statement & Solution Guide

GraphsHardHeavy-Light Decomposition
TimeO(n^2)
|
SpaceO(n)

Problem Description

Given a tree with nodes and edges, find the centroid of the tree using Heavy-Light Decomposition algorithm.

Examples

Example 1

Input

Given a tree with nodes [1, 2, 3, 4, 5] and edges [(0, 1), (0, 2), (1, 3), (1, 4)], find the centroid of the tree using Heavy-Light Decomposition algorithm.

Output

The centroid of the tree is node 1 with a sum of subtree as 1 + 2 + 3 + 4 = 10

Explanation: Step-by-step: First, we find the centroid of the tree by calculating the sum of each subtree. Then, we apply the Heavy-Light Decomposition algorithm to find the node with the maximum sum of its subtree.

Example 2

Input

Given a tree with nodes [10, 20, 30, 40, 50] and edges [(0, 1), (0, 2), (1, 3), (1, 4)], find the centroid of the tree using Heavy-Light Decomposition algorithm.

Output

The centroid of the tree is node 1 with a sum of subtree as 10 + 20 + 30 + 40 = 100

Explanation: Step-by-step: First, we find the centroid of the tree by calculating the sum of each subtree. Then, we apply the Heavy-Light Decomposition algorithm to find the node with the maximum sum of its subtree.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N log N) or O(N log^2 N)
  • Space Complexity: O(N)

Optimal Approach & Strategy

Use Heavy-Light Decomposition to process subproblems in O(N log N) time and O(N) auxiliary memory.

Brute Force Approach

Evaluate state space permutations in O(2^N) or O(N^3) time.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function solution(tree) { 
       let centroid = -1; 
       let maxSum = -1; 
       for (let i = 0; i < tree.length; i++) { 
           let sum = 0; 
           let stack = [i]; 
           let visited = new Set(); 
           while (stack.length > 0) { 
               let node = stack.pop(); 
               if (!visited.has(node)) { 
                   sum += tree[node]; 
                   visited.add(node); 
                   for (let j = 0; j < tree.length; j++) { 
                       if (tree[j] !== 0 && !visited.has(j)) { 
                           stack.push(j); 
                       } 
                   } 
               } 
           } 
           if (sum > maxSum) { 
               maxSum = sum; 
               centroid = i; 
           } 
       } 
       return centroid; 
   }

Asked in Top Tech Interviews

AppleGoldman Sachs

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.