forked from codeprepper/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.c
50 lines (41 loc) · 1.48 KB
/
answer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* problem:
* Given an input array A, print the product array B such that each element
* in B is the product of all elements in A except the element at the same
* index in A. The required time complexity is O(N) and you are NOT
* allowed to use the division operator.
*
* examples:
* [1, 2, 3, 4, 5] => [120, 60, 40, 30, 24]
*
* complexity:
* time = O(N)
* space = O(N)
**/
#include "test.h"
void productArray(int *A, int N) {
/* check input arguments */
if (A == NULL || N == 0) { return; }
/**
* construct the following temp arrays:
* L: in which each element is the product of all elements PRIOR to it
* R: in which each element is the product of all elements AFTER it
* Each element in B is then the product of the elements at the same index
* in L and R.
**/
int *L, *R, pos;
L = (int *)malloc(N * sizeof(int));
R = (int *)malloc(N * sizeof(int));
if (L == NULL || R == NULL) { return; /* malloc failure */ }
/* initialize the L array, note that the first element is 1 */
for (L[0] = 1, pos = 1; pos < N; pos++) {
L[pos] = L[pos-1] * A[pos-1]; }
/* initialize the R array, note that the last element is 1 */
for (R[N-1] = 1, pos = N-2; pos >= 0; pos--) {
R[pos] = R[pos+1] * A[pos+1]; }
/* print the product array */
for (pos = 0; pos < N; pos++) { printf("%d ", L[pos] * R[pos]); }
/* cleanup */
if (L) { free(L); }
if (R) { free(R); }
}