-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductArrayExceptSelf.java
50 lines (43 loc) · 1.5 KB
/
ProductArrayExceptSelf.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
package techQuestions;
import java.util.Arrays;
public class ProductArrayExceptSelf {
public static void main(String[] args) {
int[] nums = {4,5,1,8,2};
System.out.println(Arrays.toString(productExceptSelf(nums)));
}
public static int[] productExceptSelf(int[] nums) {
// answer with 0(N) space complexity where N is
// the number of elements in nums array
// int[] L = new int[nums.length],
// R = new int[nums.length];
// L[0] = 1;
// R[nums.length - 1] = 1;
// for (int i = 1; i < nums.length; i++) {
// L[i] = L[i-1] * nums[i-1];
// }
// for (int i = nums.length-2; i >= 0; i--) {
// R[i] = R[i+1] * nums[i+1];
// }
// what about O(1) space complexity?? don't
// allocate these extra arrays. Output/answer
// array doesn't count towards space
// complexity
int[] answer = new int[nums.length];
// initialize answer array with prod of left
answer[0] = 1;
for (int i = 1; i < nums.length; i++) {
answer[i] = answer[i-1] * nums[i-1];
}
int R = 1;
for (int i = nums.length - 2; i >= 0; i--) {
R = R * nums[i+1];
answer[i] = answer[i] * R;
}
// System.out.println(Arrays.toString(R) +
// " " + Arrays.toString(L));
// for (int i = 0; i < nums.length; i++) {
// nums[i] = L[i] * R[i];
// }
return answer;
};
}