Create two temporary arrays - B [N] and C [N]. Form each element B [N] as a product of elements A [N] to the left (including itself) - working from left to right, N operations. Form each element C [N] as a product of elements A [N] from the right (including yourself) - we work from right to left, N operations.
Then A [n] = B [n-1] * C [n + 1] is another N operations for this. As a result, you get only 3N operations, that is, O (N). It is simply short, because B [0] and C [N-1], as well as the first and last A, do not require multiplication. Also, C [0] = B [N-1], so I think you only need 3N-5 operations.
source share