Sum of Prefix Products
EasyPractice library · +10 XPdynamic programmingprefix sums
Given an array of N integers, compute and print the prefix products. The prefix product at index i, denoted as P[i], is the product of all elements from index 0 up to index i (inclusive). That is, P[i] = arr[0] * arr[1] * ... * arr[i]. Print these prefix products in order, separated by spaces.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers, the elements of the array.
Output
Print N space-separated integers, the prefix products of the given array.
Constraints
1 <= N <= 1000 -1000 <= arr[i] <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 1 2 3 4 5
Output
1 2 6 24 120
The prefix products are 1, 1*2=2, 2*3=6, 6*4=24, 24*5=120.
Example 2
Input
4 2 -1 3 0
Output
2 -2 -6 0
The prefix products are 2, 2*(-1)=-2, -2*3=-6, -6*0=0.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 3 4 5
Example 2 input
4 2 -1 3 0