FastPrepCount Winning Electoral-State Coalitions

Count Winning Electoral-State Coalitions

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each positive integer in electoralVotes is one state's electoral-vote count. A coalition wins when its total is strictly more than half of all votes.

Return the number of subsets of states that win. The empty subset is included in consideration but cannot win. States with equal vote counts remain distinct.

Return the exact count as a signed 64-bit integer.

Function

countWinningCoalitions(electoralVotes: int[]) → long

Examples

Example 1

electoralVotes = [3,2,1]return = 3

Total votes are 6; winning subsets are {3,2}, {3,1}, and {3,2,1}.

Example 2

electoralVotes = [1,1,1]return = 4

Any two states or all three states exceed half of 3.

Example 3

electoralVotes = []return = 0

No coalition can win without states.

Constraints

  • 0 <= electoralVotes.length <= 40.
  • 1 <= electoralVotes[i] <= 1000.
  • The answer fits a signed 64-bit integer.

More Google problems

See Google hiring insights
public long countWinningCoalitions(int[] electoralVotes) {
    // Write your code here.
}
electoralVotes[3,2,1]
expected3
Checking account…