Skip to content

Commit fed19b0

Browse files
committed
Add algorithmic steps for product except self problem
1 parent 02922c0 commit fed19b0

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ List of Programs related to data structures and algorithms
3636

3737
1. Contains duplicates : [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containsDuplicate.js
3838
)
39-
2. Product of array except self: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/productExceptSelf.js)
39+
2. Product of array except self: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/productExceptSelf/productExceptSelf.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/productExceptSelf/productExceptSelf.md)
4040

4141
3. Max sum subarray: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/maxSubArray.js)
4242

src/java1/algorithms/array/ProductExceptSelf.java src/java1/algorithms/array/productExceptSelf/ProductExceptSelf.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package productExceptSelf;
2+
13
import java.util.Arrays;
24

35
public class ProductExceptSelf {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of prefix and postfix pattern which includes elements multiplication. The algorithmic approach can be summarized as follows:
3+
4+
1. Initialize an empty `result` array variable to store the elements products at each index position.
5+
6+
2. Initialize `prefix` and `postfix` variables to 1, which are used to keep track on the product of the elements to the left and right of the current element.
7+
8+
3. Iterate over the input array to find the prefix for each element. At first, assign the current prefix to result element and then update the prefix value by multiplying with current element.
9+
10+
4. Iterate over the input array again but in the backward direction to find the postfix value for each element. In this case, you need to multiply the current prefix with a previously calculated result element. This is needed to hold the previous prefix values of a result element. Thereafter, you can update the postfix value by multiplying with current element.
11+
12+
5. Return `result` which contains the product of all elements other than current element.
13+
14+
15+
**Time and Space complexity:**
16+
This algorithm has a time complexity of O(n)(O(n) + O(n)), where n is the number of elements. This is because two separate for-loops which runs the array at most once.
17+
Here, we don't use any additional datastructure other than the output array(excluded as per requirement). Hence, the space complexity will be O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**Algorithmic Steps**
2+
This problem is solved with the help of prefix and postfix pattern which includes elements multiplication. The algorithmic approach can be summarized as follows:
3+
4+
1. Initialize an empty `result` array variable to store the elements products at each index position.
5+
6+
2. Initialize `prefix` and `postfix` variables to 1, which are used to keep track on the product of the elements to the left and right of the current element.
7+
8+
3. Iterate over the input array to find the prefix for each element. At first, assign the current prefix to result element and then update the prefix value by multiplying with current element.
9+
10+
4. Iterate over the input array again but in the backward direction to find the postfix value for each element. In this case, you need to multiply the current prefix with a previously calculated result element. This is needed to hold the previous prefix values of a result element. Thereafter, you can update the postfix value by multiplying with current element.
11+
12+
5. Return `result` which contains the product of all elements other than current element.
13+
14+
15+
**Time and Space complexity:**
16+
This algorithm has a time complexity of O(n)(O(n) + O(n)), where n is the number of elements. This is because two separate for-loops which runs the array at most once.
17+
Here, we don't use any additional datastructure other than the output array(excluded as per requirement). Hence, the space complexity will be O(1).

0 commit comments

Comments
 (0)