Entropy of a Node
Ashby Mutual's modellers want a second opinion on how mixed a group of claims is. Gini impurity is not the only measure in use. The other standard one is entropy, borrowed from information theory, which asks how much you still would not know about a claim pulled at random from this group. Entropy is reported in bits, and that fixes its scale exactly: a group split evenly between two outcomes scores 1, a group spread evenly across four outcomes scores 2, and a group where every claim ended the same way scores 0.
Task: Print the entropy of the group.
Input
Two lines. The first holds n, the number of possible outcomes. The second holds n whole numbers separated by single spaces — how many claims ended in each outcome. Some counts may be zero; an outcome that no claim reached adds nothing to the mixture.
Output
One line holding the entropy in bits, written with exactly 4 digits after the decimal point.
Example:
Input:
2
40 10
Output:
0.7219
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in