Cluster split majority

Write codeEasy14 minAmazon style

Your task

A coordination service runs on cluster_size machines. When the network breaks, the machines split into groups that can still reach each other, and only a group holding a strict majority of the machines the cluster was configured with may keep accepting writes. Everyone else drops to read only, which is how two halves are stopped from diverging. Each entry of scenarios describes one fault as the list of surviving group sizes, in the order a report lists them. Machines that crashed belong to no group, so the sizes in a scenario need not add up to cluster_size. For every scenario, return the 0-based position of the group that may keep accepting writes, or -1 when no group holds a majority. Return the answers in scenario order.

Examples

writable_group_index(cluster_size: int, scenarios: list[list[int]]) -> list[int]
Example 1
Inputwritable_group_index(5, [[3, 2], [2, 2], [5], [1, 1, 1]])
Output[0, -1, 0, -1]
Example 2
Inputwritable_group_index(4, [[3, 1], [2, 2], [1, 1, 1, 1]])
Output[0, -1, -1]
Example 3
Inputwritable_group_index(7, [[4], [3], [0, 4, 3]])
Output[0, -1, 1]