Problem 229460 · medium · Level 02 Linear Data Structures

Team Score Without Each Player

arrays · prefix products · suffix products

In a quiz league a team's score is the product of its players' round scores, which are whole numbers and can be zero. For every player i the captain wants to know what the team would have scored if player i had sat the round out. Given the round scores nums, return a new list out in which out[i] multiplies together every score except nums[i]. Division is not allowed. A one-player team without its only player scores 1 (the product of nothing).

Examples

Input:  nums = [2, 3, 4, 5]
Output: [60, 40, 30, 24]

Input:  nums = [2, 0, 3]
Output: [0, 6, 0]

Constraints

  • 1 <= len(nums) <= 10**5
  • Every product fits in a Python int (inputs are small).
  • Return a new list; division is not allowed.
  • Target: O(n) time.

Goals

  • Combine a prefix pass and a suffix pass
  • Avoid division so zeros need no special handling
  • Stay linear without nested loops
Starting Python…