Skip to content

Commit c348deb

Browse files
Add files via upload
1 parent 819e6ad commit c348deb

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Productexceptshelf.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Productexceptshelf {
2+
public int[] product(int[] nums) {
3+
int n = nums.length;
4+
int[] result = new int[n];
5+
int[] rightside1 = new int[n];
6+
int[] leftside1 = new int[n];
7+
int rightside = 1;
8+
int leftside = 1;
9+
10+
for (int i = 0; i < n; i++) {
11+
rightside1[i] = rightside;
12+
rightside *= nums[i];
13+
}
14+
15+
for (int i = n - 1; i >= 0; i--) {
16+
leftside1[i] = leftside;
17+
leftside *= nums[i];
18+
}
19+
20+
for (int i = 0; i < n; i++) {
21+
result[i] = leftside1[i] * rightside1[i];
22+
}
23+
24+
return result;
25+
}
26+
27+
public static void main(String[] args) {
28+
int nums[] = { 1, 2, 8, 9 };
29+
Productexceptshelf obj = new Productexceptshelf();
30+
int result[] = obj.product(nums);
31+
32+
33+
for (int num : result) {
34+
System.out.print(num + " ");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)