A neural network has n neurons numbered from 1 to n.
If the ith neuron has strong connectivity, strongConnectivity[i] = 1. If it has weak connectivity, strongConnectivity[i] = 0.
The neurons form a tree-like network with n - 1 connections, where the ith connection connects neurons neuronFrom[i] and neuronTo[i].
A neuron's strength is defined as the maximum difference between strongly connected and weakly connected neurons in any subnetwork including that neuron.
Return an array of n integers, where the ith integer represents the strength of neuron i.
Note: A subnetwork is a connected subgraph of the given network.
Examples
01 · Example 1
n = 4 neuronFrom = [1, 1, 1] neuronTo = [2, 3, 4] strongConnectivity = [0, 0, 1, 0] return = [0, -1, 1, -1]
Neuron 3 has strong connectivity.
- For neuron
1's strength, consider the subnetwork with neurons1and3:1strong,1weak, so strength is0. - For neuron
2's strength, consider the subnetwork with neurons1,2, and3:1strong,2weak, so strength is-1. - For neuron
3's strength, consider the subnetwork with only neuron3:1strong,0weak, so strength is1. - For neuron
4's strength, consider the subnetwork with only neuron4:0strong,1weak, so strength is-1.
The neuronStrengths array is [0, -1, 1, -1].
More Microsoft problems
- XOR MultiplicationOA · Seen Jun 2026
- Maximum Escape Game ScoreOA · Seen Jun 2026
- Minimum Cost K-Capable ModelsOA · Seen Jun 2026
- Rank Open BusinessesPHONE SCREEN · Seen May 2026
- Retain Top K ValuesPHONE SCREEN · Seen May 2026
- In-Memory SQL with CSV InitializationONSITE INTERVIEW · Seen May 2026
- Order Records by Matching Start and EndONSITE INTERVIEW · Seen May 2026
- Recover Corrupted Master PageONSITE INTERVIEW · Seen Feb 2026
public int[] getNeuronStrengths(int n, int[] neuronFrom, int[] neuronTo, int[] strongConnectivity) {
// write your code here
}
n4
neuronFrom[1, 1, 1]
neuronTo[2, 3, 4]
strongConnectivity[0, 0, 1, 0]
expected[0, -1, 1, -1]
checking account