Build suffix products17.02
Same shape as suffix sums, but multiply. Watch zeros: a zero at i makes suffix product 0 for all j≤i.
prod[n]=1, prod[i]=a[i]*prod[i+1]. Identity of * is 1, not 0. The trailing 1 is the empty product of the suffix past n-1.
Same skill, three traces. Read all three before you try it.
01no zero - given
a = [2, 3, 4]- walk
from right:
1,4,12,24→prod=[24, 12, 4, 1]- result
[24, 12, 4, 1]
02a zero - given
a = [2, 0, 4]- walk
prod[3]=1,prod[2]=4,prod[1]=0,prod[0]=0- result
[0, 0, 4, 1]
03one - given
a = [7]- walk
prod=[7, 1]- result
[7, 1]
suffixProd(a).a is an array of integers. n is a.length.Build prod with
prod[n]=1 and prod[i]=a[i]*prod[i+1]. Index n is 1 because the empty product is 1, not 0.[2, 3, 4] → [24, 12, 4, 1]. [2, 0, 4] → [0, 0, 4, 1]. [7] → [7, 1].constraintsa is a non-empty integer array.
- Product of Array Except Self0/3 skills ready · arrays · mediumopen
- Product of Array Except Self0/3 skills ready · arrays · mediumopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.