-
Notifications
You must be signed in to change notification settings - Fork 0
/
238-ProductOfArrayExceptSelf.java
46 lines (40 loc) · 1.29 KB
/
238-ProductOfArrayExceptSelf.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
// 238. Product of Array Except Self
// Space Complexity - O(N * N)
class Solution {
public int[] productExceptSelf(int[] nums) {
int [] outputArray = new int[nums.length];
int numberOfLoops = nums.length;
int product = 1;
int outputIndex = 0;
while(numberOfLoops > 0) {
for (int i=0; i < nums.length; i++) {
if (i != outputIndex) {
product = product * nums[i];
}
}
outputArray[outputIndex] = product;
product = 1;
outputIndex++;
numberOfLoops--;
}
return outputArray;
}
}
Time Complexity - O(N), Space Complexity - O(1)
class Solution {
public int[] productExceptSelf(int[] nums) {
int [] outputArray = new int[nums.length];
outputArray[0] = 1;
int product = 1;
// calculate all the product to the left side of i
for(int i = 1; i < nums.length; i++) {
outputArray[i] = outputArray[i-1] * nums[i-1];
}
int right = 1;
for(int i = nums.length-1; i >= 0; i--) {
outputArray[i] = outputArray[i] * right;
right = right * nums[i];
}
return outputArray;
}
}