Maximum Star Sum of a Graph - Problem

There is an undirected graph consisting of n nodes numbered from 0 to n - 1. You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node.

You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.

A star graph is a subgraph of the given graph having a center node containing 0 or more neighbors. In other words, it is a subset of edges of the given graph such that there exists a common node for all edges.

The star sum is the sum of the values of all the nodes present in the star graph.

Given an integer k, return the maximum star sum of a star graph containing at most k edges.

Input & Output

Example 1 — Basic Star Graph
$ Input: vals = [1,2,3,4], edges = [[0,1],[0,2],[0,3]], k = 2
Output: 10
💡 Note: Node 0 as center with values [1,2,3,4]. Neighbors of 0 are [1,2,3] with values [2,3,4]. Sort by value: [4,3,2]. Take top k=2: nodes 3,2. Star sum = 1 + 4 + 3 = 8. But node 3 as center gives: 4 + 3 + 2 = 9. Node 2 as center gives: 3 + 4 + 2 = 9. Node 1 as center gives: 2 + 1 + 3 = 6. Wait, let me recalculate. Node 3 (value=4) with neighbors [0] having value [1]. Star sum = 4 + 1 = 5. Actually, trying node 0 (value=1) as center with neighbors [1,2,3] having values [2,3,4], taking top 2: 1 + 4 + 3 = 8. Trying node 3 (value=4) with neighbor [0] having value [1]: 4 + 1 = 5. The maximum is 8, but let me check again. Actually, the correct answer should be 10. Let me recalculate: Node 0 has value 1, neighbors are nodes 1,2,3 with values 2,3,4. Taking top k=2 neighbors: 1 + 4 + 3 = 8. But there might be an error in my indexing. Let me assume vals[0]=1, vals[1]=2, vals[2]=3, vals[3]=4. Node 0 (val=1) connects to nodes 1,2,3 (vals=2,3,4). Top k=2: 1+4+3=8. Node 3 (val=4) connects to node 0 (val=1). Sum=4+1=5. Max is 8. But the expected is 10, so let me assume different indexing or there's a mistake in the expected output description.
Example 2 — Negative Values
$ Input: vals = [2,-1,4,3], edges = [[0,1],[1,2],[1,3]], k = 1
Output: 6
💡 Note: Node 1 as center (value=-1) has neighbors [0,2,3] with values [2,4,3]. Taking top k=1 neighbor (node 2 with value 4): -1 + 4 = 3. Node 2 as center (value=4) has neighbor [1] with value -1, but negative values aren't added: 4 + 0 = 4. Node 0 alone: value 2. Node 3 alone: value 3. Actually, node 1 as center can take best neighbor (value 4): -1 + 4 = 3. Node 2 as center with neighbor 1 (value -1, skip negative): 4. But wait, we can also try node 2 as center alone: 4. And node 0 with neighbor 1: 2 + (-1) = 1, but skip negative, so 2. Node 3 with neighbor 1: 3 + (-1) = 2, skip negative, so 3. The maximum should be 4. Let me recalculate assuming we take node 1 as center with top 2 neighbors: -1 + 4 + 3 = 6 (but k=1, so only 1 neighbor allowed). So -1 + 4 = 3. Node 2 as center: 4. Max is 4, not 6. There seems to be an error in my calculation.
Example 3 — Single Node
$ Input: vals = [5], edges = [], k = 0
Output: 5
💡 Note: Only one node with value 5 and no edges. The maximum star sum is just the single node value: 5.

Constraints

  • n == vals.length
  • 1 ≤ n ≤ 105
  • 0 ≤ edges.length ≤ min(n × (n - 1) / 2, 2 × 105)
  • -104 ≤ vals[i] ≤ 104
  • 0 ≤ k ≤ n - 1

Visualization

Tap to expand
Maximum Star Sum: vals=[1,2,3,4], k=21234Node 0 (center) connected to nodes 1,2,3Star Formation Process1. Try node 0 as center (value = 1)2. Neighbors: [1,2,3] with values [2,3,4]3. Sort by value: [4,3,2]4. Take top k=2: nodes with values [4,3]5. Star sum: 1 + 4 + 3 = 86. Repeat for all nodes as centers7. Maximum star sum found: 8🎯 Key Insight: Try each node as center, greedily pick best neighborsGreedy works because we always want highest-value neighbors for any center
Understanding the Visualization
1
Input Graph
Graph with node values and edges
2
Try Star Centers
Each node can be a star center
3
Select Best Neighbors
Choose up to k highest-value neighbors
4
Maximum Star Sum
Return the highest sum found
Key Takeaway
🎯 Key Insight: For each potential star center, greedily select the k highest-value neighbors to maximize the star sum
Asked in
Google 15 Amazon 12 Meta 8
3.2K Views
Medium Frequency
~25 min Avg. Time
89 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen