Giả sử chúng ta có một mảng gọi là NUM, chúng ta phải tìm sản phẩm của các phần tử của một subarray liền kề trong một mảng [chứa ít nhất một số] có sản phẩm lớn nhất. Vì vậy, nếu mảng là [1,9,2,0,2,5], đầu ra sẽ là 18, vì subarray tiếp giáp [1,9,2] có sản phẩm tối đa.
Để giải quyết vấn đề này, chúng tôi sẽ làm theo các bước này -
- MAX_LIST: = Danh sách kích thước NUMS và điền vào 0
- MIN_LIST: = Danh sách kích thước NUMS và điền vào 0
- MIN_LIST: = Danh sách kích thước NUMS và điền vào 0
- Đối với tôi trong phạm vi từ 1 đến chiều dài của nums
- MAX_LIST [i] = max của max_list [i - 1]*nums [i], min_list [i - 1]*nums [i] và nums [i]
- min_list [i] = minof min_list [i - 1]*nums [i], nums [i], max_list [i - 1]*nums [i]
- Trả lại Max của Max_List
Hãy cho chúng tôi xem việc thực hiện sau đây để hiểu rõ hơn -
Thí dụ
& nbsp; bản demo trực tiếp
class Solution[object]: def maxProduct[self, nums]: max_list = [0] * len[nums] min_list = [0] * len[nums] max_list[0] = nums[0] min_list[0] = nums[0] for i in range[1,len[nums]]: max_list[i] = max[max[max_list[i-1]*nums[i],min_list[i-1]*nums[i]],nums[i]] min_list[i] = min[min[min_list[i-1]*nums[i],nums[i]],max_list[i-1]*nums[i]] return max[max_list] ob1 = Solution[] print[ob1.maxProduct[[1,9,2,0,2,5]]]
Đầu vào
[1,9,2,0,2,5]
Đầu ra
18
Cập nhật vào ngày 20 tháng 10 năm 2020 07:43:33
- Câu hỏi và câu trả lời liên quan
- Chương trình tìm sản phẩm tối đa Subarray Min trong Python
- Chương trình C ++ để tìm tối đa của từng con subarray tiếp giáp kích thước k
- Chương trình tìm độ dài tối đa của subarray với sản phẩm tích cực trong Python
- Subarray sản phẩm tối đa trong Python
- Tổng số tiếp giáp của subarray trong javascript
- Chương trình tìm tổng số phụ liên tục với số tiền tối đa trong Python
- Chương trình tìm tổng số tuyệt đối tối đa của bất kỳ subarray nào trong Python
- Chương trình tìm điểm tối đa của một Subarray tốt trong Python
- Sản phẩm lớn nhất của các chữ số tiếp giáp trong Python
- Chương trình tìm Subarray tăng dần tối đa bằng cách sử dụng Python
- Subarray sản phẩm tối đa | Đã thêm trường hợp sản phẩm tiêu cực trong C ++
- Subarray tiếp giáp lớn nhất
- Chương trình C/C ++ cho Subarray tiếp giáp lớn nhất?
- Subarray sản phẩm tối đa - Sử dụng hai lần đi ngang trong C ++
- Chương trình tìm tổng số tối đa của modulo subarray bằng m trong python
Đưa ra một mảng chứa cả số nguyên dương và âm, hãy tìm sản phẩm của sản phẩm tối đa Subarray. Độ phức tạp thời gian dự kiến là O [N] và chỉ có thể sử dụng không gian O [1] O [1].
Examples:
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}
Giải pháp ngây thơ:
Ý tưởng là đi qua mọi subarrays tiếp giáp, tìm sản phẩm của mỗi subarrays này và trả lại sản phẩm tối đa từ các kết quả này.
Dưới đây là việc thực hiện phương pháp trên.
C++
#include
using
namespace
[1,9,2,0,2,5]0
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
180
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
185
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}0
188
182
183
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}5
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}9
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1121
188
Maximum Sub array product is 1123
188
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}9
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 1120
Maximum Sub array product is 1123
[1,9,2,0,2,5]1
Maximum Sub array product is 1123
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 1127
‘
[1,9,2,0,2,5]8
Maximum Sub array product is 1126
Maximum Sub array product is 1127
Maximum Sub array product is 1128
Maximum Sub array product is 1129
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
#include
2Maximum Sub array product is 1123
C
#include
4
[1,9,2,0,2,5]1
#include
6[1,9,2,0,2,5]1
#include
8[1,9,2,0,2,5]1
using
0[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
using
4Maximum Sub array product is 1123
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
180
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
185
188
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}0
188
182
183
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}5
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}9
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1121
188
Maximum Sub array product is 1123
188
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}9
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 1120
Maximum Sub array product is 1123
[1,9,2,0,2,5]1
Maximum Sub array product is 1123
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 1127
‘
[1,9,2,0,2,5]8
[1,9,2,0,2,5]37
183
[1,9,2,0,2,5]39
[1,9,2,0,2,5]40
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
#include
2Maximum Sub array product is 1123
C
[1,9,2,0,2,5]1
#include
6[1,9,2,0,2,5]1
#include
8[1,9,2,0,2,5]1
using
0[1,9,2,0,2,5]8
Maximum Sub array product is 1129
using
4[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]00
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]08
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 1127
Java
[1,9,2,0,2,5]45
[1,9,2,0,2,5]46
[1,9,2,0,2,5]47
[1,9,2,0,2,5]48
[1,9,2,0,2,5]82
[1,9,2,0,2,5]83
[1,9,2,0,2,5]82
Maximum Sub array product is 1121
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]83
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]54
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]59
[1,9,2,0,2,5]60
[1,9,2,0,2,5]61
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]64
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]69
[1,9,2,0,2,5]60
[1,9,2,0,2,5]71
188
1822
Maximum Sub array product is 1127
1824
1825
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
Maximum Sub array product is 1123
Python3
1829
1830
[1,9,2,0,2,5]8
1832
1833
1834
[1,9,2,0,2,5]60
1836
[1,9,2,0,2,5]8
182
1839
1840
1841
1842
188
1844
1833
1846
188
182
1849
1840
1841__
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
1832
1833
1859
1860
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
1844
1863
1833
1865
188
1832
1833
1859
1860
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
1873
1874
1833
1876
[1,9,2,0,2,5]80
181212
1895
1833
1897
1898
1899
183
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}01
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}02
C#
using
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}04
[1,9,2,0,2,5]47
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}06
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}11
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
180
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}18
[1,9,2,0,2,5]8
182223
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}23
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}0
188
182223
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}33
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}37
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1121
188
Maximum Sub array product is 1123
188
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}37
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 1120
Maximum Sub array product is 1123
[1,9,2,0,2,5]98
[1,9,2,0,2,5]50
1800
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}53
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}57
[1,9,2,0,2,5]8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}59
Maximum Sub array product is 1127
1853
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}62
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}63
Maximum Sub array product is 1123
Maximum Sub array product is 1123
JavaScript
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}66
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}68
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}71
[1,9,2,0,2,5]8
182
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}74
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}78
188
182
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}81
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]83
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1121
188
Maximum Sub array product is 1123
188
[1,9,2,0,2,5]83
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 1120
Maximum Sub array product is 1123
[1,9,2,0,2,5]98
[1,9,2,0,2,5]50
1800
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}53
[1,9,2,0,2,5]8
Maximum Sub array product is 11201
[1,9,2,0,2,5]8
Maximum Sub array product is 11203
Maximum Sub array product is 1127
188
Maximum Sub array product is 11206
Maximum Sub array product is 11207
[1,9,2,0,2,5]
8[1,9,2,0,2,5]
1 Input: arr[] = {6, -3, -10, 0, 2}
Output: 180 // The subarray is {6, -3, -10}
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60 // The subarray is {60}
Input: arr[] = {-2, -40, 0, -2, -3}
Output: 80 // The subarray is {-2, -40}
57
Maximum Sub array product is 112
[1,9,2,0,2,5]
8Input: arr[] = {6, -3, -10, 0, 2}
Output: 180 // The subarray is {6, -3, -10}
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60 // The subarray is {60}
Input: arr[] = {-2, -40, 0, -2, -3}
Output: 80 // The subarray is {-2, -40}
59Maximum Sub array product is 112
7 18
53 O[N2]
Auxiliary Space: O[1]
JavaScript
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}68
It is similar to Largest Sum Contiguous Subarray problem. The only thing to note here is, maximum product can also be obtained by minimum [negative] product ending with the previous element multiplied by this element. For example, in array {12, 2, -3, -5, -6, -2}, when we are at element -2, the maximum product is multiplication of, minimum product ending with -6 and -2.
[1,9,2,0,2,5]
818
2 Input: arr[] = {6, -3, -10, 0, 2}
Output: 180 // The subarray is {6, -3, -10}
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60 // The subarray is {60}
Input: arr[] = {-2, -40, 0, -2, -3}
Output: 80 // The subarray is {-2, -40}
74:
if all elements of array are negative then the maximum product with the above algorithm is 1. so, if maximum product is 1, then we have to return the maximum element of an array.
C++
#include
188
182
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}81
[1,9,2,0,2,5]8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}99
[1,9,2,0,2,5]7
Đầu ra
Độ phức tạp về thời gian: O [N2] Không gian phụ trợ: O [1]
Giải pháp hiệu quả:
Giải pháp sau đây giả định rằng mảng đầu vào đã cho luôn có đầu ra dương. Các giải pháp hoạt động cho tất cả các trường hợp được đề cập ở trên. Nó không hoạt động cho các mảng như {0, 0, -20, 0}, {0, 0, 0} .. v.v ... Giải pháp có thể dễ dàng sửa đổi để xử lý trường hợp này. & Nbsp; nó tương tự như tổng lớn nhất Vấn đề Subarray. Điều duy nhất cần lưu ý ở đây là, sản phẩm tối đa cũng có thể được lấy bằng sản phẩm tối thiểu [âm] kết thúc với phần tử trước đó nhân với phần tử này. Ví dụ: trong mảng {12, 2, -3, -5, -6, -2}, khi chúng ta ở phần tử -2, sản phẩm tối đa là phép nhân của sản phẩm tối thiểu kết thúc với -6 và -2. & NBSP;
Lưu ý: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Nếu tất cả các phần tử của mảng là âm thì sản phẩm tối đa với thuật toán trên là 1. Vì vậy, nếu sản phẩm tối đa là 1, thì chúng ta phải trả về phần tử tối đa của một mảng. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
using
namespace
[1,9,2,0,2,5]0
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11244
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11246
[1,9,2,0,2,5]82
Maximum Sub array product is 11248
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11250
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11221
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11221
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11224
188
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11224
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11227
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11270
[1,9,2,0,2,5]82
Maximum Sub array product is 11272
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11274
188
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11230
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11281
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}23
188
Maximum Sub array product is 11239
Maximum Sub array product is 11240
188
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
Maximum Sub array product is 11295
Maximum Sub array product is 11296
Maximum Sub array product is 11295
182223
[1,9,2,0,2,5]1
Maximum Sub array product is 11201
Maximum Sub array product is 1128
Maximum Sub array product is 11203
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
Maximum Sub array product is 1123
[1,9,2,0,2,5]1
Maximum Sub array product is 1123
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 1127
‘
[1,9,2,0,2,5]8
Maximum Sub array product is 1126
Maximum Sub array product is 1127
Maximum Sub array product is 1128
Maximum Sub array product is 1129
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
#include
2Maximum Sub array product is 1123
C
#include
4
[1,9,2,0,2,5]1
Maximum Sub array product is 11234
[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
Maximum Sub array product is 11238
Maximum Sub array product is 1129
Maximum Sub array product is 11240
[1,9,2,0,2,5]1
#include
6[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
Maximum Sub array product is 11238
Maximum Sub array product is 1129
Maximum Sub array product is 11248
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11221
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11224
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11227
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11230
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]00
188
Maximum Sub array product is 11239
Maximum Sub array product is 11275
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11244
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11246
[1,9,2,0,2,5]82
Maximum Sub array product is 11248
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11250
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11254
Maximum Sub array product is 11239
Maximum Sub array product is 11256
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11221
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11224
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11254
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]1
Maximum Sub array product is 11268
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11270
[1,9,2,0,2,5]82
Maximum Sub array product is 11272
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11274
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11239
Maximum Sub array product is 11279
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11281
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 11239
Maximum Sub array product is 11286
188
Maximum Sub array product is 1129
#include
2[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
Maximum Sub array product is 1123
[1,9,2,0,2,5]1
Maximum Sub array product is 1123
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 1127
‘
[1,9,2,0,2,5]8
[1,9,2,0,2,5]37
183
Maximum Sub array product is 11246
Maximum Sub array product is 11247
Maximum Sub array product is 11248
Maximum Sub array product is 11249
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
#include
2Maximum Sub array product is 1123
C
[1,9,2,0,2,5]1
Maximum Sub array product is 11234
[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
Maximum Sub array product is 11238
Maximum Sub array product is 1129
Maximum Sub array product is 11240
[1,9,2,0,2,5]1
#include
6[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
Maximum Sub array product is 11238
Maximum Sub array product is 1129
Maximum Sub array product is 11248
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11221
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11224
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11227
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11230
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]00
188
Maximum Sub array product is 11239
Maximum Sub array product is 11275
188
Maximum Sub array product is 11254
Maximum Sub array product is 11239
Maximum Sub array product is 11256
188
Maximum Sub array product is 11254
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]1
Maximum Sub array product is 11268
188
Maximum Sub array product is 11239
Maximum Sub array product is 11279
188
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
Maximum Sub array product is 11239
Maximum Sub array product is 11286
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]7
[1,9,2,0,2,5]82
Maximum Sub array product is 11244
[1,9,2,0,2,5]82
Maximum Sub array product is 11246
#include
35#include
36
[1,9,2,0,2,5]80
#include
38[1,9,2,0,2,5]82
#include
12[1,9,2,0,2,5]80
Maximum Sub array product is 11299
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
188
Maximum Sub array product is 1129
#include
2Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]7
[1,9,2,0,2,5]82
Maximum Sub array product is 11297
[1,9,2,0,2,5]80
Maximum Sub array product is 11299
[1,9,2,0,2,5]82
#include
02[1,9,2,0,2,5]80
Maximum Sub array product is 11299
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
Java
[1,9,2,0,2,5]45
[1,9,2,0,2,5]46
[1,9,2,0,2,5]82
Maximum Sub array product is 11270
[1,9,2,0,2,5]47
Maximum Sub array product is 11257
[1,9,2,0,2,5]82
Maximum Sub array product is 11274
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
Maximum Sub array product is 11234
[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
Maximum Sub array product is 11265
[1,9,2,0,2,5]82
Maximum Sub array product is 11281
188
Maximum Sub array product is 1123
Maximum Sub array product is 112666
Maximum Sub array product is 1129
Maximum Sub array product is 11268
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
#include
6[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
Maximum Sub array product is 11265
Maximum Sub array product is 112666
Maximum Sub array product is 1129
Maximum Sub array product is 11281
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]54
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]64
188
1822
Maximum Sub array product is 1127
1824
1825
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
using
33
Python3
188
[1,9,2,0,2,5]1
Maximum Sub array product is 11297
[1,9,2,0,2,5]80
Maximum Sub array product is 11299
188
[1,9,2,0,2,5]1
#include
022[1,9,2,0,2,5]80
Maximum Sub array product is 11299
188
[1,9,2,0,2,5]1
#include
07[1,9,2,0,2,5]60
Maximum Sub array product is 11299
188
[1,9,2,0,2,5]1
#include
12[1,9,2,0,2,5]60
Maximum Sub array product is 11299
188
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]69
[1,9,2,0,2,5]60
#include
21Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11239
#include
26[1,9,2,0,2,5]60
#include
28Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11254
Maximum Sub array product is 11239
#include
48[1,9,2,0,2,5]60
#include
28Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11254
[1,9,2,0,2,5]7
[1,9,2,0,2,5]82
[1,9,2,0,2,5]1
Maximum Sub array product is 11268
#include
35#include
2
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11239
Maximum Sub array product is 11279
188
Maximum Sub array product is 11239
#include
88[1,9,2,0,2,5]60
#include
90____[1,9,2,0,2,5]60
#include
92Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1129
[1,9,2,0,2,5]60
Maximum Sub array product is 11299
188
Maximum Sub array product is 1129
Maximum Sub array product is 11208
188
Maximum Sub array product is 11254
using
69[1,9,2,0,2,5]8
[1,9,2,0,2,5]98
[1,9,2,0,2,5]50
1800
1801
Các
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
using
461833
namespace
081863
1846
188
Maximum Sub array product is 11239
namespace
28Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
using
501833
Maximum Sub array product is 11270
Is
188
Maximum Sub array product is 1129
[1,9,2,0,2,5]60
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
namespace
501874
1833
namespace
53[1,9,2,0,2,5]80____2121212
1899
183
namespace
74namespace
75C#
using
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}04
[1,9,2,0,2,5]47
[1,9,2,0,2,5]48
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
Maximum Sub array product is 11234
[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
namespace
87[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
Maximum Sub array product is 11295
Maximum Sub array product is 1129
Maximum Sub array product is 11268
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
#include
6[1,9,2,0,2,5]1
Maximum Sub array product is 11236
[1,9,2,0,2,5]1
namespace
87[1,9,2,0,2,5]8
[1,9,2,0,2,5]004
Maximum Sub array product is 11266
Maximum Sub array product is 1129
[1,9,2,0,2,5]007
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]015
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}18
188
[1,9,2,0,2,5]1
Maximum Sub array product is 11221
188
[1,9,2,0,2,5]1
Maximum Sub array product is 11224
188
[1,9,2,0,2,5]1
Maximum Sub array product is 11227
188
[1,9,2,0,2,5]1
Maximum Sub array product is 11230
188
182
183
[1,9,2,0,2,5]1
185
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11239
Maximum Sub array product is 11275
[1,9,2,0,2,5]82
Maximum Sub array product is 11244
[1,9,2,0,2,5]82
[1,9,2,0,2,5]046
[1,9,2,0,2,5]047
[1,9,2,0,2,5]048
[1,9,2,0,2,5]049
[1,9,2,0,2,5]050
[1,9,2,0,2,5]82
Maximum Sub array product is 11250
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11254
Maximum Sub array product is 11239
[1,9,2,0,2,5]058
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]7
[1,9,2,0,2,5]82
Maximum Sub array product is 11221
[1,9,2,0,2,5]82
Maximum Sub array product is 11224
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11254
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]7
[1,9,2,0,2,5]82
[1,9,2,0,2,5]1
Maximum Sub array product is 11268
[1,9,2,0,2,5]82
[1,9,2,0,2,5]075
[1,9,2,0,2,5]047
[1,9,2,0,2,5]048
[1,9,2,0,2,5]049
[1,9,2,0,2,5]050
[1,9,2,0,2,5]82
Maximum Sub array product is 11274
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1123
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11239
Maximum Sub array product is 11279
[1,9,2,0,2,5]82
Maximum Sub array product is 11281
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11239
Maximum Sub array product is 11286
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 1129
#include
2188
Maximum Sub array product is 1129
Maximum Sub array product is 11208
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]98
[1,9,2,0,2,5]50
1800
[1,9,2,0,2,5]106
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]111
188
[1,9,2,0,2,5]113
Maximum Sub array product is 1127
[1,9,2,0,2,5]115
1825
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
Maximum Sub array product is 1123
PHP
[1,9,2,0,2,5]120
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
[1,9,2,0,2,5]1222
[1,9,2,0,2,5]123
181212
[1,9,2,0,2,5]125
#include
28[1,9,2,0,2,5]7
Is
Maximum Sub array product is 1123
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
[1,9,2,0,2,5]140
[1,9,2,0,2,5]123
181212
[1,9,2,0,2,5]125
#include
28[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
[1,9,2,0,2,5]123
[1,9,2,0,2,5]149
[1,9,2,0,2,5]125
[1,9,2,0,2,5]133___
Maximum Sub array product is 1123
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
[1,9,2,0,2,5]2
[1,9,2,0,2,5]159
181212____1161
#include
92[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]165
[1,9,2,0,2,5]166
[1,9,2,0,2,5]8
[1,9,2,0,2,5]168
[1,9,2,0,2,5]166
[1,9,2,0,2,5]8
[1,9,2,0,2,5]171
[1,9,2,0,2,5]172
[1,9,2,0,2,5]8
[1,9,2,0,2,5]174
[1,9,2,0,2,5]172
Is
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
Maximum Sub array product is 11239
183
[1,9,2,0,2,5]159
namespace
53[1,9,2,0,2,5]179
[1,9,2,0,2,5]195
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]165
[1,9,2,0,2,5]200
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]165
[1,9,2,0,2,5]203
[1,9,2,0,2,5]159
namespace
53[1,9,2,0,2,5]179
[1,9,2,0,2,5]61
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]168
[1,9,2,0,2,5]200
[1,9,2,0,2,5]82
[1,9,2,0,2,5]212
[1,9,2,0,2,5]168
[1,9,2,0,2,5]214
[1,9,2,0,2,5]203
[1,9,2,0,2,5]159
namespace
53[1,9,2,0,2,5]179
[1,9,2,0,2,5]219
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]174
[1,9,2,0,2,5]166
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11254
Maximum Sub array product is 11239
183
[1,9,2,0,2,5]159
namespace
53[1,9,2,0,2,5]179
[1,9,2,0,2,5]232
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]165
[1,9,2,0,2,5]166
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]168
[1,9,2,0,2,5]166
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11254
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]248
[1,9,2,0,2,5]249
[1,9,2,0,2,5]165
Maximum Sub array product is 11299
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]165
1833
[1,9,2,0,2,5]82
[1,9,2,0,2,5]256
[1,9,2,0,2,5]168
[1,9,2,0,2,5]214
[1,9,2,0,2,5]203
[1,9,2,0,2,5]159
namespace
53[1,9,2,0,2,5]179
[1,9,2,0,2,5]219
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]168
1833
[1,9,2,0,2,5]214
[1,9,2,0,2,5]248
[1,9,2,0,2,5]203
[1,9,2,0,2,5]159____953
[1,9,2,0,2,5]179
[1,9,2,0,2,5]61
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11239
183
[1,9,2,0,2,5]171
[1,9,2,0,2,5]131
[1,9,2,0,2,5]165
#include
92Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]171
[1,9,2,0,2,5]249
[1,9,2,0,2,5]165
Maximum Sub array product is 11299
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
Các
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
[1,9,2,0,2,5]171
Maximum Sub array product is 11299
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]159
[1,9,2,0,2,5]249
[1,9,2,0,2,5]307
[1,9,2,0,2,5]308
[1,9,2,0,2,5]8
[1,9,2,0,2,5]161
[1,9,2,0,2,5]311
[1,9,2,0,2,5]159
[1,9,2,0,2,5]313
[1,9,2,0,2,5]159
[1,9,2,0,2,5]315
[1,9,2,0,2,5]8
[1,9,2,0,2,5]317
183
Maximum Sub array product is 1127
#include
38[1,9,2,0,2,5]8
[1,9,2,0,2,5]317
[1,9,2,0,2,5]323
[1,9,2,0,2,5]159__212121216161327
[1,9,2,0,2,5]328
JavaScript
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}66
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}68
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]334
[1,9,2,0,2,5]8
[1,9,2,0,2,5]336
[1,9,2,0,2,5]8
[1,9,2,0,2,5]338
[1,9,2,0,2,5]8
[1,9,2,0,2,5]340
[1,9,2,0,2,5]8
182
[1,9,2,0,2,5]343
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
Maximum Sub array product is 11239
Maximum Sub array product is 11240
188
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11244
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11246
[1,9,2,0,2,5]82
[1,9,2,0,2,5]356
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11250
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11254
Maximum Sub array product is 11239
Maximum Sub array product is 11256
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11221
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11224
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11254
[1,9,2,0,2,5]7
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]375
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11270
[1,9,2,0,2,5]82
[1,9,2,0,2,5]379
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11274
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 11239
Maximum Sub array product is 11279
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
Maximum Sub array product is 11281
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 11239
Maximum Sub array product is 11286
188
Maximum Sub array product is 1129
#include
2[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}99
[1,9,2,0,2,5]8
Maximum Sub array product is 11201
[1,9,2,0,2,5]8
Maximum Sub array product is 11203
Maximum Sub array product is 1127
[1,9,2,0,2,5]408
#include
35
[1,9,2,0,2,5]410
Maximum Sub array product is 11207
Đầu ra
Maximum Sub array product is 112
Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [1] O[n]
Auxiliary Space: O[1]
& nbsp; giải pháp hiệu quả:Efficient Solution:
Giải pháp trên giả định luôn có một kết quả tích cực cho mảng đã cho không hoạt động cho các trường hợp trong đó mảng chỉ chứa các phần tử không dương như {0, 0, -20, 0}, {0, 0, 0} .. v.v ... Giải pháp sửa đổi cũng tương tự như vấn đề Subarray tiếp giáp lớn nhất sử dụng thuật toán của Kadane. Để dễ hiểu, chúng tôi không sử dụng bất kỳ cờ nào như giải pháp trước đó. Ở đây chúng tôi sử dụng 3 biến được gọi là max_so_far, max_ending_here & min_ending_here. Đối với mỗi chỉ mục, số lượng tối đa kết thúc tại chỉ mục đó sẽ là mức tối đa [mảng [i], max_ending_here * mảng [i], min_ending_here [i] * mảng [i]]. Tương tự, số lượng tối thiểu kết thúc ở đây sẽ là mức tối thiểu của những thứ này 3. Do đó, chúng tôi nhận được giá trị cuối cùng cho sản phẩm tối đa Subarray.
C++
#include
using
namespace
[1,9,2,0,2,5]0
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]425
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]428
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11296
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]436
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]439
[1,9,2,0,2,5]440
[1,9,2,0,2,5]441
188
Maximum Sub array product is 11246
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}8
[1,9,2,0,2,5]445
#include
35
[1,9,2,0,2,5]441
188
[1,9,2,0,2,5]449
188
[1,9,2,0,2,5]451
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 11203
Maximum Sub array product is 1127
[1,9,2,0,2,5]408
[1,9,2,0,2,5]7
Đầu ra
Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [1]
[1,9,2,0,2,5]8
Maximum Sub array product is 1126
Maximum Sub array product is 1127
Maximum Sub array product is 1128
Maximum Sub array product is 1129
& nbsp; giải pháp hiệu quả:
Maximum Sub array product is 1123
Giải pháp trên giả định luôn có một kết quả tích cực cho mảng đã cho không hoạt động cho các trường hợp trong đó mảng chỉ chứa các phần tử không dương như {0, 0, -20, 0}, {0, 0, 0} .. v.v ... Giải pháp sửa đổi cũng tương tự như vấn đề Subarray tiếp giáp lớn nhất sử dụng thuật toán của Kadane. Để dễ hiểu, chúng tôi không sử dụng bất kỳ cờ nào như giải pháp trước đó. Ở đây chúng tôi sử dụng 3 biến được gọi là max_so_far, max_ending_here & min_ending_here. Đối với mỗi chỉ mục, số lượng tối đa kết thúc tại chỉ mục đó sẽ là mức tối đa [mảng [i], max_ending_here * mảng [i], min_ending_here [i] * mảng [i]]. Tương tự, số lượng tối thiểu kết thúc ở đây sẽ là mức tối thiểu của những thứ này 3. Do đó, chúng tôi nhận được giá trị cuối cùng cho sản phẩm tối đa Subarray.
#include
4
using
namespace
[1,9,2,0,2,5]0
[1,9,2,0,2,5]7
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]425
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]428
Maximum Sub array product is 1123
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]425
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]428
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11296
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]436
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]439
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}62
[1,9,2,0,2,5]528
188
[1,9,2,0,2,5]530
[1,9,2,0,2,5]531
[1,9,2,0,2,5]528
[1,9,2,0,2,5]8
[1,9,2,0,2,5]449
[1,9,2,0,2,5]8
[1,9,2,0,2,5]451
Maximum Sub array product is 1123
188
[1,9,2,0,2,5]451
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 11203
Maximum Sub array product is 1127
[1,9,2,0,2,5]408
[1,9,2,0,2,5]7
Đầu ra
Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [1]
[1,9,2,0,2,5]8
[1,9,2,0,2,5]37
183
Maximum Sub array product is 11246
Maximum Sub array product is 11247
Maximum Sub array product is 11248
Maximum Sub array product is 11249
& nbsp; giải pháp hiệu quả:
Maximum Sub array product is 1123
Giải pháp trên giả định luôn có một kết quả tích cực cho mảng đã cho không hoạt động cho các trường hợp trong đó mảng chỉ chứa các phần tử không dương như {0, 0, -20, 0}, {0, 0, 0} .. v.v ... Giải pháp sửa đổi cũng tương tự như vấn đề Subarray tiếp giáp lớn nhất sử dụng thuật toán của Kadane. Để dễ hiểu, chúng tôi không sử dụng bất kỳ cờ nào như giải pháp trước đó. Ở đây chúng tôi sử dụng 3 biến được gọi là max_so_far, max_ending_here & min_ending_here. Đối với mỗi chỉ mục, số lượng tối đa kết thúc tại chỉ mục đó sẽ là mức tối đa [mảng [i], max_ending_here * mảng [i], min_ending_here [i] * mảng [i]]. Tương tự, số lượng tối thiểu kết thúc ở đây sẽ là mức tối thiểu của những thứ này 3. Do đó, chúng tôi nhận được giá trị cuối cùng cho sản phẩm tối đa Subarray.
using
namespace
[1,9,2,0,2,5]0
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]425
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
[1,9,2,0,2,5]428
[1,9,2,0,2,5]8
[1,9,2,0,2,5]1
Maximum Sub array product is 11296
[1,9,2,0,2,5]8
182
183
[1,9,2,0,2,5]1
[1,9,2,0,2,5]436
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]439
188
[1,9,2,0,2,5]451
Maximum Sub array product is 11266
[1,9,2,0,2,5]604
Maximum Sub array product is 11266
[1,9,2,0,2,5]449
Maximum Sub array product is 11266
[1,9,2,0,2,5]608
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
[1,9,2,0,2,5]570
[1,9,2,0,2,5]615
[1,9,2,0,2,5]8
Maximum Sub array product is 11203
Maximum Sub array product is 1127
[1,9,2,0,2,5]408
[1,9,2,0,2,5]570
[1,9,2,0,2,5]7
Đầu ra
Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [1]
[1,9,2,0,2,5]8
[1,9,2,0,2,5]644
Maximum Sub array product is 11246
[1,9,2,0,2,5]646
[1,9,2,0,2,5]570
Maximum Sub array product is 1123
Maximum Sub array product is 1123
Python3
& nbsp; giải pháp hiệu quả:
Giải pháp trên giả định luôn có một kết quả tích cực cho mảng đã cho không hoạt động cho các trường hợp trong đó mảng chỉ chứa các phần tử không dương như {0, 0, -20, 0}, {0, 0, 0} .. v.v ... Giải pháp sửa đổi cũng tương tự như vấn đề Subarray tiếp giáp lớn nhất sử dụng thuật toán của Kadane. Để dễ hiểu, chúng tôi không sử dụng bất kỳ cờ nào như giải pháp trước đó. Ở đây chúng tôi sử dụng 3 biến được gọi là max_so_far, max_ending_here & min_ending_here. Đối với mỗi chỉ mục, số lượng tối đa kết thúc tại chỉ mục đó sẽ là mức tối đa [mảng [i], max_ending_here * mảng [i], min_ending_here [i] * mảng [i]]. Tương tự, số lượng tối thiểu kết thúc ở đây sẽ là mức tối thiểu của những thứ này 3. Do đó, chúng tôi nhận được giá trị cuối cùng cho sản phẩm tối đa Subarray.
using
namespace
[1,9,2,0,2,5]0
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]4
[1,9,2,0,2,5]1
[1,9,2,0,2,5]6
[1,9,2,0,2,5]8
182
1839
1840
1841
183
[1,9,2,0,2,5]80
1855
188
namespace
081833
1859
183__
188
using
461833
using
79183__
188
using
421833
[1,9,2,0,2,5]703
188
using
501833
1859
[1,9,2,0,2,5]708
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
namespace
501874
1833
1876
[1,9,2,0,2,5]80
181212
1895
1833
1897
1898
1899
[1,9,2,0,2,5]738
[1,9,2,0,2,5]739
#include
92JavaScript
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}66
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}67
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}68
[1,9,2,0,2,5]7
[1,9,2,0,2,5]8
[1,9,2,0,2,5]746
[1,9,2,0,2,5]8
[1,9,2,0,2,5]748
[1,9,2,0,2,5]8
[1,9,2,0,2,5]750
[1,9,2,0,2,5]8
182
[1,9,2,0,2,5]753
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]757
188
[1,9,2,0,2,5]604
188
[1,9,2,0,2,5]449
188
[1,9,2,0,2,5]608
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
Maximum Sub array product is 1129
Maximum Sub array product is 11208
Maximum Sub array product is 1123
[1,9,2,0,2,5]770
[1,9,2,0,2,5]771
Maximum Sub array product is 11203
Maximum Sub array product is 1127
[1,9,2,0,2,5]774
Maximum Sub array product is 11207
C#
using
Input: arr[] = {6, -3, -10, 0, 2} Output: 180 // The subarray is {6, -3, -10} Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 // The subarray is {60} Input: arr[] = {-2, -40, 0, -2, -3} Output: 80 // The subarray is {-2, -40}04
[1,9,2,0,2,5]47
[1,9,2,0,2,5]48
[1,9,2,0,2,5]8
[1,9,2,0,2,5]50
[1,9,2,0,2,5]1
[1,9,2,0,2,5]2
[1,9,2,0,2,5]1
[1,9,2,0,2,5]015
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]425
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]428
188
[1,9,2,0,2,5]1
Maximum Sub array product is 11296
188
182223
[1,9,2,0,2,5]1
[1,9,2,0,2,5]801
188
[1,9,2,0,2,5]7
[1,9,2,0,2,5]804
[1,9,2,0,2,5]1
[1,9,2,0,2,5]806
[1,9,2,0,2,5]804
[1,9,2,0,2,5]808
[1,9,2,0,2,5]804
[1,9,2,0,2,5]449
[1,9,2,0,2,5]804
[1,9,2,0,2,5]812
188
Maximum Sub array product is 1123
188
Maximum Sub array product is 1129
Maximum Sub array product is 11208
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
[1,9,2,0,2,5]8
[1,9,2,0,2,5]98
[1,9,2,0,2,5]50
1800
[1,9,2,0,2,5]106
[1,9,2,0,2,5]8
[1,9,2,0,2,5]7
188
[1,9,2,0,2,5]1
[1,9,2,0,2,5]111
188
[1,9,2,0,2,5]113
Maximum Sub array product is 1127
[1,9,2,0,2,5]214
1825
[1,9,2,0,2,5]8
Maximum Sub array product is 1123
Maximum Sub array product is 1123
Đầu ra
Maximum Sub array product is 112
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [1] O[N]
Auxiliary Space: O[1]
Bài viết này được biên soạn bởi Dheeraj Jain và được xem xét bởi nhóm GeekSforGeeks. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trênDheeraj Jain and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above