28) given an array a [n], we want to construct array B [N], where B [J] = A [0] * A [1]… A [N-1]/A [J], division is not allowed during construction:
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, heap space, and Global static variables) cannot be used );
Implementation Program (any mainstream programming language) Implementation and simple description.
Ignore any restrictions on the subject. Imagine what solutions can be imagined?
1. the simplest way is to multiply A [0] to a [N-1], then traverse the array, divide by a [J] in turn, this is the simplest way, however, division is not allowed. This method is not feasible.
2. since division cannot be used, we can use another way of thinking: First multiply the number on the left of a [J], and then multiply the number on the right of a [J, then, we can avoid division and obtain the value of a [J. in this way, it is obviously required that every A [I] needs to traverse the array, the time complexity is O (n), after all a [0] to a [N-1], the time complexity is O (n ^ 2 ). does not match the question. however, this method provides us with the possibility that we can combine a layer-1 nested loop in a layer-2 nested loop? This problem is unknown for the time being, but we can know that we can implement this O (N ^ 2) method first, and then see if we can reduce the time complexity.
Code for O (N ^ 2) time redundancy:
void ConstructArray(int* A,int* B,int N){for(int i=0;i<N;++i){B[i]=1;for(int j=0;j<N;++j){if(j != i){B[i]=B[i]*A[j];}}}}
Namely: | is the split line.
Table: 1-1
A [0] = 1 | * A [1] * A [2] * ...... * A [N-1]
A [1] = A [0] * | * A [2] * A [3] ...... A [N-1]
A [2] = A [0] * A [1] * | * A [3] *... A [N-1]
......
A [J] = A [0 * A [1] *... A [J-1] | A [J + 1] * A [J + 2] *... A [N-1]
The above code implementation idea is like this: divide the problem into two parts: for a [0], a [1] ...... to each a [I] of a [N-1], the result is obtained through repeating the array. in this way
Layer-2 nesting.
Now, we need to change the angle to see if we can calculate the split line | the results on both sides are all saved in an array, and then multiply the two arrays to solve the problem. in the middle, we do not need two temporary arrays. We can use the features of the B [N] array to complete this accumulation process.
Void constructarray (int * a, int * B, int N) {If (! A |! B) return; B [0] = 1; for (INT I = 0; I <= N-2; ++ I) {B [I + 1] = B [I] * A [I]; // comparison table: 1-1 easy to know, construct the left part of the split line} // the left part of the split line has been created on the top of a pyramid, now from the bottom up to repeat the above operation can establish the right split line part for (INT I = N-1; i> = 1; -- I) {B [I] = B [I] * B [0]; B [0] = B [0] * A [I] ;}}
Summary:
1. The question is demanding. The way of thinking is to use B [I] to construct an array of the Left pyramid type, and then construct the right pyramid type. The intermediate conversion is clever.