FastPrepNext Pascal Row Coefficient

Next Pascal Row Coefficient

Intuit logoIntuit● EasyFULLTIMEOA

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[]) → int

Examples

Example 1

prefix = [1,4,6,4]return = 1

The row is [1,4,6,4,1], so its next coefficient is1.

Example 2

prefix = [1,5,10]return = 10

The row is [1,5,10,10,5,1]. After the supplied three coefficients, the next coefficient is10.

Example 3

prefix = [1,3]return = 3

The 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).

More Intuit problems

See Intuit hiring insights
Saved
public int nextPascal(int[] prefix) {
    // Write your solution here.
}
prefix[1,4,6,4]
expected1
Checking account…