-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathproductExceptSelf.java
More file actions
32 lines (22 loc) · 872 Bytes
/
Copy pathproductExceptSelf.java
File metadata and controls
32 lines (22 loc) · 872 Bytes
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
import java.util.Arrays;
// product of two arrays consisting the multiplicating their left preceders and right aheads except them
class productExceptSelf {
public static void main(String[] args) {
int[] input = {1,-1,2,4,7,-2,8,-3}; //
int[] output = products(input);
System.out.println(Arrays.toString(output)); //[-2688, 2688, -1344, -672, -384, 1344, -336, 896]
}
static int[] products(int[] nums) {
int[] left=new int[nums.length];
int[] right = new int[nums.length];
left[0]=1;
right[nums.length-1]=1;
for(int i=1;i<nums.length;i++)
left[i]=nums[i-1]*left[i-1];
for(int i=nums.length-2;i>=0;i--)
right[i]=nums[i+1]*right[i+1];
for(int i=0;i<nums.length;i++)
nums[i]=left[i]*right[i];
return nums;
}
}