Child array Product

Source: Internet
Author: User

Given an array a [n], we want to construct array B [N], where B [I] = A [0] * A [1] *... * A [N-1]/A [I]. In the construction process:
Division is not allowed;
Requires O (1) space complexity and O (n) time complexity;
In addition to traversing the counter and a [n] B [N], new variables (including stack temporary variables, space and global static variables) cannot be used );

UseProgramImplementation and simple description.

First look at the implementationCode:

 
Void product (int A [], int B [], int N) {B [0] = 1; for (INT I = 1; I <n; I ++) B [I] = B [I-1] * A [I-1]; for (INT I = N-1; I> = 1; I --) {B [I] = B [I] * B [0]; B [0] = B [0] * A [I];} return ;}

In the first loop, for each loop I, B [I] saves a [0] * A [1] *... * A [I-1].
In the second loop, for each loop I, start B [0] to save a [I + 1] * A [I + 2] *... * A [N-1], B [I] = B [I] * B [0] is ( A [0] * A [1] *... * A [I-1]) * (a [I + 1] * A [I + 2] *... * A [N-1]), updating B [0] at the same time. By the end of the loop, B [0] is also a [1] * A [2] *... * A [N-1].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.