Skip to content

Commit 0999d34

Browse files
committed
Add product of array except self solution
1 parent bf97331 commit 0999d34

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
fun productExceptSelf(nums: IntArray): IntArray =
3+
IntArray(nums.size) { 1 }.apply {
4+
var leftProduct = 1
5+
for (i in 1 until nums.size) {
6+
leftProduct *= nums[i - 1]
7+
this[i] = leftProduct
8+
}
9+
10+
var rightProduct = 1
11+
for (i in nums.size - 2 downTo 0) {
12+
rightProduct *= nums[i + 1]
13+
this[i] *= rightProduct
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)