Cluster split majority
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.
writable_group_index(cluster_size: int, scenarios: list[list[int]]) → list[int][5,[[3,2],[2,2],[5],[1,1,1]]]out[0,-1,0,-1][4,[[3,1],[2,2],[1,1,1,1]]]out[0,-1,-1][7,[[4],[3],[0,4,3]]]out[0,-1,1]State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
[5,[[3,2],[2,2],[5],[1,1,1]]][0,-1,0,-1]not run yetsample[4,[[3,1],[2,2],[1,1,1,1]]][0,-1,-1]not run yetsample[7,[[4],[3],[0,4,3]]][0,-1,1]not run yetsample