Next Pascal Row Coefficient
Problem statement
You are given a prefix of one row of Pascal’s triangle. Row n starts [1,n,...] and its coefficient at index i is the binomial coefficient C(n,i).
Return the next coefficient after the supplied prefix. If the prefix already contains the entire row, return -1. Valid inputs include two-element prefixes such as [1,3]; rows0 and1 are outside this exercise.
Function
nextPascal(prefix: int[]) → intExamples
Example 1
prefix = [1,4,6,4]return = 1The row is [1,4,6,4,1], so its next coefficient is1.
Example 2
prefix = [1,5,10]return = 10The row is [1,5,10,10,5,1]. After the supplied three coefficients, the next coefficient is10.
Example 3
prefix = [1,3]return = 3The row is [1,3,3,1]. A two-element prefix is valid here, and the next coefficient is3.
Constraints
- 2 <= prefix.length <= 31.
- 1 <= prefix[i] <= 155117520.
- prefix is a valid initial segment of Pascal row n for some 2 <= n <= 30, where coefficient i is C(n,i).