-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayProductsWithoutDivision.java
119 lines (104 loc) · 3.12 KB
/
ArrayProductsWithoutDivision.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Given an array of numbers, produce a new array where each element is the
* product of all elements in the original array except the current one.
*
* The catch is: do this without using division.
*/
public class ArrayProductsWithoutDivision {
/**
* Naively generate a new int[] where each element is the product of all other
* elements. Runs in O(N^2) time using O(N) space.
*
* @param arr - input int[]
* @return new int[] where each element is the product of all other elements of
* arr
*/
public static int[] naive(int[] arr) {
int[] products = new int[arr.length];
int product;
for (int i = 0; i < arr.length; i++) {
product = 1;
for (int j = 0; j < arr.length; j++) {
if (i != j) {
product *= arr[j];
}
}
products[i] = product;
}
return products;
}
/**
* Efficiently generate a new int[] where each element is the product of all
* other elements. Runs in O(N) time using O(N) space.
*
* Calculates prefix and suffix arrays wherein the ith element of each is equal
* to the running product of all values to either the left or the right of i
* from arr.
*
* @param arr - input int[]
* @return new int[] where each element is the product of all other elements of
* arr
*/
public static int[] memo(int[] arr) {
int len = arr.length;
int[] prefixes = new int[len];
int[] suffixes = new int[len];
int[] products = new int[len];
// find prefixes
int product = 1;
for (int i = 0; i < len; i++) {
prefixes[i] = product;
product *= arr[i];
}
// find suffixes
product = 1;
for (int i = len - 1; i >= 0; i--) {
suffixes[i] = product;
product *= arr[i];
}
// calc product for each index using prefix and suffix
for (int i = 0; i < len; i++) {
product = 1;
product *= prefixes[i];
product *= suffixes[i];
products[i] = product;
}
return products;
}
/**
* Helper to print out an int array.
* @param arr - int[] to print out
* @return - String representation of arr
*/
public static String printArr(int[] arr) {
StringBuilder str = new StringBuilder();
str.append("[");
for (int i = 0; i < arr.length - 1; i++) {
str.append(arr[i]);
str.append(", ");
}
str.append(arr[arr.length - 1]);
str.append("]");
return str.toString();
}
public static void main(String[] args) {
// random int array where elements range from -50 to 50
int[] arr = new int[100000];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100) - 50;
}
long start, end;
int[] val;
System.out.println("Input Array Length:\t" + arr.length);
// memo
start = System.currentTimeMillis();
val = memo(arr);
end = System.currentTimeMillis();
System.out.println(String.format("Memo Calculation Time:\t%d ms", end - start));
// maive
start = System.currentTimeMillis();
val = naive(arr);
end = System.currentTimeMillis();
System.out.println(String.format("Naive Calculation Time:\t%d ms", end - start));
}
}