You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.md
+33-17Lines changed: 33 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -7,35 +7,51 @@ Input: nums = [6, 7,-4, 5, 8, ]
7
7
Output: 6
8
8
9
9
Example 2:
10
-
Input: nums = [-2,0,-2]
10
+
Input: nums = [-2, 0, -2]
11
11
Output: 0
12
12
13
-
**Algorithmic Steps:**
13
+
## Algorithmic Steps
14
14
15
-
This problem is solved with the help of **Kadane's algorithm**(dynamic programming technique). The algorithmic approach can be summarized as follows:
15
+
This problem is efficiently solved using a variation of **Kadane's Algorithm** tailored for products. It tracks both the **maximum** and **minimum** product ending at each position, due to the behavior of negative numbers.
16
16
17
-
1. Initialize the maximum product subarray(`result`) with a first number.
17
+
1.**Initialize** three variables with the first element of the array `nums[0]`:
18
+
-`result`: Holds the maximum product found so far.
19
+
-`currentMax`: Tracks the maximum product ending at the current index.
20
+
-`currentMin`: Tracks the minimum product ending at the current index.
18
21
19
-
**Note:**It is not suggested to assign the value to either 0 or 1 because the maximum value can be less than or equal to 0.
22
+
> **Note:**We initialize all three with the first element instead of 1 or 0 to properly account for arrays with negative or single values.
20
23
21
-
2.Initialize two variables named `currentMax` and `currentMin` to `1`. They are used to keep track of the running maximum and minimum products.
24
+
2.**Iterate** over the array starting from the second element to last element:
22
25
23
-
3. Iterate over the entire input array using for-of loop
26
+
3. For each element `num`:
27
+
- Compute temporary products using the previous `currentMax` and `currentMin`:
28
+
-`tempMax = currentMax * num`
29
+
-`tempMin = currentMin * num`
24
30
25
-
4. Find the temporary maximum and minimum products for each current element.
31
+
4.**Update**`currentMax` by taking the maximum among:
32
+
-`num` (start new subarray)
33
+
-`tempMax`
34
+
-`tempMin`
35
+
This ensures that the current maximum product is correctly tracked.
26
36
27
-
5. Calculate the maximum value between maximum and minimum values of step4, and this calculated maximum result needs to be compared with current element to find the final current maximum product.
37
+
5.**Update**`currentMin` by taking the minimum among:
38
+
-`num`
39
+
-`tempMax`
40
+
-`tempMin`
41
+
This is crucial for handling negative values, which could turn a future product into a maximum.
28
42
29
-
**Note:** Since an array contain both positive and negative integers, the product of current element with minimum product calculated so far may result into maximum value. That is the reason why we took maximum of current maximum and current minimum proucts.
43
+
6.**Update** the global `result` by taking the maximum of:
44
+
-`result`
45
+
-`currentMax`
46
+
This ensures the highest product is captured across the entire array.
30
47
31
-
6. Calculate the minimum value between maximum and minimum values of step4, and this calculated minimum result needs to be compared with current element to find the final current minimum product. This current minimum product is useful to calculate the maximum value for the next iteration.
48
+
7. After the loop completes, **return** the `result`, which contains the maximum product of any contiguous subarray.
32
49
33
-
7. Update the global maximum product by taking the maximum of current maximum product and global maximum product of previous iteration.
50
+
---
34
51
35
-
8. Return the global maximum `result` after the end of for-each iteration.
52
+
## Time and Space Complexity
36
53
37
-
**Time and Space complexity:**
54
+
-**Time Complexity**: `O(n)` — We traverse the input array once.
55
+
-**Space Complexity**: `O(1)` — No extra space is used except for a few variables.
38
56
39
-
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we iterate the array at most once.
40
-
41
-
Here, we don't use any additional datastructure other than the two sum variables. Hence, the space complexity will be O(1).
Copy file name to clipboardExpand all lines: src/javascript/algorithms/array/9.minRotatedSortedarray/minRotatedSortedarray.md
+23-14Lines changed: 23 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
**Description:**
2
2
Given a sorted array `nums` of length `n` with all unique elements, in which the array is rotated between 1 and n times. Return the minimum element of this array.
3
3
4
-
**Note:** You need to write an algorithm that runs in O(log n) time.
4
+
**Note:** You need to write an algorithm that runs in `O(log n)` time.
5
5
6
6
### Examples
7
7
Example 1:
@@ -14,27 +14,36 @@ Output: 0
14
14
15
15
**Algorithmic Steps:**
16
16
17
-
This problem is solved with the help of **binary search** technique. Since it is a rotated sorted array, there will be two subararys of elements in an ascending order. Once you find the correct subarray, the left most element is the minimum value. The algorithmic approach can be summarized as follows:
17
+
This problem can be efficiently solved using the **binary search** technique. Because the array is a rotated version of a sorted array, it consists of two ascending subarrays. By identifying which subarray contains the minimum element, we can narrow down the search. The minimum value will always be the leftmost element of the correctly identified subarray. The algorithmic approach can be summarized as follows:
18
18
19
-
1. Initialize the minimum value(`result`) to first element of an array.
20
19
21
-
2. Add preliminary checks by returning the first element if the length of array is one, or minimum of first two values if the length of an array is two.
20
+
1.**Initialize the Result:**
21
+
- Set the initial minimum value `result` to the first element of the array.
22
22
23
-
3. Initialize left and right pointers(`left` and `right`) to first index and last index of an array.
23
+
2.**Handle Base Cases:**
24
+
- If the array length is `1`, return the only element.
25
+
- If the array length is `2`, return the smaller of the two elements.
24
26
25
-
4. Iterate over an input array using while loop with the condition of left pointer is less than or equal to right pointer.
27
+
3.**Set Search Boundaries:**
28
+
- Initialize two pointers: `left` at the start (index 0), and `right` at the end (index `n - 1`) of the array.
26
29
27
-
5. If the left element is less than or equal to right element, update the result value and break the while loop to return the minimum value.
30
+
4.**Iterate Using Binary Search:**
31
+
- While `left < right`:
32
+
- Check if the subarray between `left` and `right` is already sorted:
33
+
- If `nums[left] < nums[right]`, return `nums[left]` as the minimum.
6. Calculate the middle index of an array to separate the sorted subarrays.
36
+
5.**Decide Which Half to Search:**
37
+
- If `nums[mid] > nums[right]`:
38
+
- The minimum lies in the **right half**.
39
+
- Move `left` pointer to `mid + 1`.
40
+
- Otherwise:
41
+
- The minimum lies in the **left half** (including `mid`).
42
+
- Move `right` pointer to `mid`.
30
43
31
-
7. If the middle value is greater than right most element, the minimum value exists with in the right side subarray. So the left pointer will be updated to next element of middle element.
44
+
6.**Return Minimum:**
45
+
- After the loop ends, `nums[left]` holds the minimum element in the rotated sorted array.
32
46
33
-
8. If the middle value is less than or equal to left most element, the minimum value exists with in the left side subarray. So the right pointer will be updated to the middle element.
34
-
35
-
9. Repeat steps 4-7 until you find the left most value which is minimum in the entire input array.
36
-
37
-
10. Return the result element after the end of an iteration.
0 commit comments