Skip to content

Commit

Permalink
Product of Array Except Self
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed May 19, 2024
1 parent 9e83353 commit 08b8e29
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/0238-Product-Of-Array-Except-Self.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/)

## Intuition

The problem asks for the product of all elements in the array except the one at the current index. This can be done efficiently by keeping track of prefix and postfix products separately. By avoiding the use of division, we ensure that our solution is robust and handles edge cases like zeros in the array.

## Approach

1. **Prefix and Postfix Arrays:**
- First, we create two auxiliary arrays, prefixes and postfixes, where `prefixes[i]` contains the product of all elements to the left of `i` and `postfixes[i]` contains the product of all elements to the right of `i`.
- Initialize the first element of prefixes to `1` because there are no elements to the left of the first element.
- Initialize the last element of postfixes to `1` because there are no elements to the right of the last element.
2. **Filling Prefix and Postfix Arrays:**
- Traverse the input array to fill the prefixes array such that each element at index `i` in prefixes is the product of all elements to the left of `i`.
- Simultaneously, traverse the input array in reverse to fill the postfixes array such that each element at index `i` in postfixes is the product of all elements to the right of `i`.
3. **Combining Results:** Create the result array where each element at index i is the product of `prefixes[i]` and `postfixes[i]`, giving the desired product of all elements except `nums[i]`.
4. **Space Optimization:** he second method, `productExceptSelfWithExtraSpaceComplexity`, optimizes space by combining the steps for prefixes and postfixes into a single array, thereby reducing the space complexity to `O(1)` additional space.

## Complexity

- **Time Complexity:**
- **`O(N)`** for both methods, where `N` is the length of the input array. Each method involves traversing the array a few times, which results in linear time complexity.
- **Time Complexity:**
- For the first method, `productExceptSelf`, the space complexity is `O(N)` due to the use of the prefixes and postfixes arrays.
- For the second method, `productExceptSelfWithExtraSpaceComplexity`, the space complexity is `O(1)` additional space since we only use a single array for the result and a few extra variables.

## Code

- [Java](/src/main/java/io/dksifoua/leetcode/productofarrayexceptself/Solution.java)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.dksifoua.leetcode.productofarrayexceptself;

public class Solution {

public int[] productExceptSelf(int[] nums) {
int[] prefixes = new int[nums.length];
int[] postfixes = new int[nums.length];
int[] result = new int[nums.length];

prefixes[0] = 1;
postfixes[nums.length - 1] = 1;
for (int i = 1, j = nums.length - 2; i < nums.length && j >= 0; i++, j--) {
prefixes[i] = prefixes[i - 1] * nums[i - 1];
postfixes[j] = postfixes[j + 1] * nums[j + 1];
}

for (int i = 0; i < nums.length; i++) {
result[i] = prefixes[i] * postfixes[i];
}

return result;
}

public int[] productExceptSelfWithExtraSpaceComplexity(int[] nums) {
int[] result = new int[nums.length];

result[0] = 1;
for (int i = 1; i < nums.length; i++) {
result[i] = result[i - 1] * nums[i - 1];
}

int postfix = 1;
for (int i = nums.length - 1; i >= 0; i--) {
if (i < nums.length - 1) {
postfix *= nums[i + 1];
}
result[i] *= postfix;
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.dksifoua.leetcode.productofarrayexceptself;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SolutionTest {

public final Solution solution = new Solution();

@Test
void test1() {
int[] input = { 1, 2, 3, 4 };
int[] expect = { 24, 12, 8, 6 };
int[] actual = solution.productExceptSelf(input);
Assertions.assertArrayEquals(expect, actual);
}

@Test
void test2() {
int[] input = { -1, 1, 0, -3, 3 };
int[] expect = { 0, 0, 9, 0, 0 };
int[] actual = solution.productExceptSelf(input);
Assertions.assertArrayEquals(expect, actual);
}

@Test
void test3() {
int[] input = { 1, 2, 3, 4 };
int[] expect = { 24, 12, 8, 6 };
int[] actual = solution.productExceptSelfWithExtraSpaceComplexity(input);
Assertions.assertArrayEquals(expect, actual);
}

@Test
void test4() {
int[] input = { -1, 1, 0, -3, 3 };
int[] expect = { 0, 0, 9, 0, 0 };
int[] actual = solution.productExceptSelfWithExtraSpaceComplexity(input);
Assertions.assertArrayEquals(expect, actual);
}
}

0 comments on commit 08b8e29

Please sign in to comment.