-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.java
More file actions
31 lines (28 loc) · 881 Bytes
/
tempCodeRunnerFile.java
File metadata and controls
31 lines (28 loc) · 881 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
public class ProductofArrayExceptSelf {
public int[] producExceptSelf(int[] nums){
int n = nums.length;
int[] right = new int [n];
int product = 1;
for(int i=n-1; i>=0; i--){
product = product * nums[i];
right[i] = product;
}
int [] ans = new int[n];
int left = 1;
for(int i=0; i<n-1; i++){
int val = left * nums[i+1];
ans [i] = val;
left = left * nums[i];
}
ans[n-1] = left;
return ans;
}
public static void main(String[] args) {
ProductofArrayExceptSelf obj = new ProductofArrayExceptSelf();
int[] nums = {1, 2, 3, 4};
int[] result = obj.producExceptSelf(nums);
for (int val : result) {
System.out.print(val + " ");
}
}
}