Skip to content

Commit b0e36e1

Browse files
committed
Create ProductExceptItself.java
1 parent a309412 commit b0e36e1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class ProductExceptItself {
2+
public static int[] productExceptItself(int[] nums){
3+
int n = nums.length;
4+
int[] result = new int[n];
5+
int leftProduct =1;
6+
for(int i=0;i<n;i++){
7+
result[i] = leftProduct;
8+
leftProduct *= nums[i];
9+
}
10+
int rightProduct = 1;
11+
for(int i=n-1;i>=0;i--){
12+
result[i] *= rightProduct;
13+
rightProduct *= nums[i];
14+
}
15+
return result;
16+
}
17+
public static void main(String[] args){
18+
int[] nums = {1,2,3,-1,4,5,0,1,-1};
19+
int[] result = productExceptItself(nums);
20+
System.out.println("Product of array except itself:");
21+
for(int num : result){
22+
System.out.print(num + " ");
23+
}
24+
System.out.println();
25+
}
26+
}

0 commit comments

Comments
 (0)