Hướng dẫn max product of contiguous sequence in python - sản phẩm tối đa của trình tự liền kề trong python


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.

Show

Để 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

Hướng dẫn max product of contiguous sequence in python - sản phẩm tối đa của trình tự liền kề trong python

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.

Hướng dẫn max product of contiguous sequence in python - sản phẩm tối đa của trình tự liền kề trong python

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
18
0

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
18
5

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

18
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}
0

18
8
18
2
18
3
[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

18
8
[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 112
1

18
8
Maximum Sub array product is 112
3

18
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

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
0

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
1
Maximum Sub array product is 112
3

[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 112
7

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
6
Maximum Sub array product is 112
7

Maximum Sub array product is 112
8
Maximum Sub array product is 112
9

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 #include 2

Maximum Sub array product is 112
3

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 using0

[1,9,2,0,2,5]
7

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 using4

Maximum Sub array product is 112
3

[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
18
0

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
18
5

18
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}
0

18
8
18
2
18
3
[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 112
1

18
8
Maximum Sub array product is 112
3

18
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

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
0

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
1
Maximum Sub array product is 112
3

[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 112
7

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
37
18
3
[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 112
9 #include 2

Maximum Sub array product is 112
3

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 using0

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 using4

[1,9,2,0,2,5]
8
18
2
18
3
[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

18
8
18
2
18
3
[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 112
7

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 112
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}
8
Maximum Sub array product is 112
3

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

18
8
[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 112
3

18
8
[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

18
8
18
2
18
3
[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

18
8
18
22
Maximum Sub array product is 112
7

18
24
18
25

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

Maximum Sub array product is 112
3

Python3

18
29
18
30

[1,9,2,0,2,5]
8
18
32
18
33
18
34
[1,9,2,0,2,5]
60
18
36

[1,9,2,0,2,5]
8
18
2
18
39
18
40
18
41
18
42

18
8
18
44
18
33
18
46

18
8
18
2
18
49
18
40
18
41__

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
18
32
18
33
18
59
18
60

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
18
44
18
63
18
33
18
65

18
8
18
32
18
33
18
59
18
60

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
18
73

18
74
18
33
18
76
[1,9,2,0,2,5]
80
18
1212

18
95
18
33
18
97
18
98

18
99
18
3
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
18
0

[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
18
2223
[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

18
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}
0

18
8
18
2223
[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

18
8
[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 112
1

18
8
Maximum Sub array product is 112
3

18
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

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
0

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
98
[1,9,2,0,2,5]
50
18
00
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 112
7
18
53

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 112
3

Maximum Sub array product is 112
3

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
18
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

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

18
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}
78

18
8
18
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}
81

18
8
[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 112
1

18
8
Maximum Sub array product is 112
3

18
8
[1,9,2,0,2,5]
83

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
0

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
98
[1,9,2,0,2,5]
50
18
00
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 112
01

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
03
Maximum Sub array product is 112
7

18
8
Maximum Sub array product is 112
06

Maximum Sub array product is 112
07

[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]
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 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]
8
18
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

18
8
18
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}
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

18
8
[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 112
44

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 112
46

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
48

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 112
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]
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 112
21

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 112
21

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 112
24

18
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
24

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
27

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 112
70

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
72

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 112
74

18
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
30

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 112
81

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
18
2
18
3
[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

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
40

18
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

Maximum Sub array product is 112
95
Maximum Sub array product is 112
96

Maximum Sub array product is 112
95
18
2223
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
01

Maximum Sub array product is 112
8
Maximum Sub array product is 112
03

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
1
Maximum Sub array product is 112
3

[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 112
7

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
6
Maximum Sub array product is 112
7

Maximum Sub array product is 112
8
Maximum Sub array product is 112
9

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 #include 2

Maximum Sub array product is 112
3

C

#include 4

[1,9,2,0,2,5]
1
Maximum Sub array product is 112
34
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
36
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
38
Maximum Sub array product is 112
9
Maximum Sub array product is 112
40

[1,9,2,0,2,5]
1 #include 6
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
36
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
38
Maximum Sub array product is 112
9
Maximum Sub array product is 112
48

[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 112
21

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
24

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
27

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
30

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
00

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
75

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 112
44

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 112
46

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
48

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 112
50

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
54
Maximum Sub array product is 112
39
Maximum Sub array product is 112
56

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 112
21

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 112
24

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
54
[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 112
68

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 112
70

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
72

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 112
74

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
79

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 112
81

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
86

18
8
Maximum Sub array product is 112
9 #include 2

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
1
Maximum Sub array product is 112
3

[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 112
7

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
37
18
3
Maximum Sub array product is 112
46
Maximum Sub array product is 112
47

Maximum Sub array product is 112
48
Maximum Sub array product is 112
49

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 #include 2

Maximum Sub array product is 112
3

C

[1,9,2,0,2,5]
1
Maximum Sub array product is 112
34
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
36
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
38
Maximum Sub array product is 112
9
Maximum Sub array product is 112
40

[1,9,2,0,2,5]
1 #include 6
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
36
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
38
Maximum Sub array product is 112
9
Maximum Sub array product is 112
48

[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 112
21

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
24

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
27

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
30

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
00

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
75

18
8
Maximum Sub array product is 112
54
Maximum Sub array product is 112
39
Maximum Sub array product is 112
56

18
8
Maximum Sub array product is 112
54
[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 112
68

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
79

18
8
[1,9,2,0,2,5]
7

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
86

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 112
44

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
46

#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 112
99

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 112
3

18
8
Maximum Sub array product is 112
9 #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
[1,9,2,0,2,5]
7

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
97
[1,9,2,0,2,5]
80
Maximum Sub array product is 112
99

[1,9,2,0,2,5]
82#include 02
[1,9,2,0,2,5]
80
Maximum Sub array product is 112
99

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 112
3

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 112
70

[1,9,2,0,2,5]
47
Maximum Sub array product is 112
57

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
74

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 112
3

[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 112
34
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
36
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
65

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
81

18
8
Maximum Sub array product is 112
3

Maximum Sub array product is 112
666
Maximum Sub array product is 112
9
Maximum Sub array product is 112
68

[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 112
36
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
65

Maximum Sub array product is 112
666
Maximum Sub array product is 112
9
Maximum Sub array product is 112
81

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[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

18
8
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
64

18
8
18
22
Maximum Sub array product is 112
7

18
24
18
25

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

using33

Python3

18
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
97
[1,9,2,0,2,5]
80
Maximum Sub array product is 112
99

18
8
[1,9,2,0,2,5]
1 #include 022
[1,9,2,0,2,5]
80
Maximum Sub array product is 112
99

18
8
[1,9,2,0,2,5]
1 #include 07
[1,9,2,0,2,5]
60
Maximum Sub array product is 112
99

18
8
[1,9,2,0,2,5]
1 #include 12
[1,9,2,0,2,5]
60
Maximum Sub array product is 112
99

18
8
18
2
18
3
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
69
[1,9,2,0,2,5]
60#include 21

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 112
39 #include 26
[1,9,2,0,2,5]
60#include 28

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 112
54
Maximum Sub array product is 112
39 #include 48
[1,9,2,0,2,5]
60#include 28

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 112
54
[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 112
68

#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 112
39
Maximum Sub array product is 112
79

18
8
Maximum Sub array product is 112
39 #include 88
[1,9,2,0,2,5]
60 #include 90____
[1,9,2,0,2,5]
60#include 92

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 112
9
[1,9,2,0,2,5]
60
Maximum Sub array product is 112
99

18
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

18
8
Maximum Sub array product is 112
54using69

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
98
[1,9,2,0,2,5]
50
18
00
18
01

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}
8using46
18
33 namespace08
18
63
18
46

18
8
Maximum Sub array product is 112
39 namespace28

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}
8using50
18
33
Maximum Sub array product is 112
70

Is

18
8
Maximum Sub array product is 112
9
[1,9,2,0,2,5]
60

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 namespace50

18
74
18
33 namespace53
[1,9,2,0,2,5]
80____2121212

18
99
18
3namespace74namespace75

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
Maximum Sub array product is 112
34
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
36
[1,9,2,0,2,5]
1 namespace87

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

Maximum Sub array product is 112
95
Maximum Sub array product is 112
9
Maximum Sub array product is 112
68

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[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 112
36
[1,9,2,0,2,5]
1 namespace87

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
004

Maximum Sub array product is 112
66
Maximum Sub array product is 112
9
[1,9,2,0,2,5]
007

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[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

18
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

18
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
21

18
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
24

18
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
27

18
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
30

18
8
18
2
18
3
[1,9,2,0,2,5]
1
18
5

18
8
[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 112
39
Maximum Sub array product is 112
75

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
44

[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 112
50

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 112
3

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 112
54
Maximum Sub array product is 112
39
[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 112
21

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
24

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 112
3

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 112
54

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 112
68

[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 112
74

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 112
3

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 112
39
Maximum Sub array product is 112
79

[1,9,2,0,2,5]
82
Maximum Sub array product is 112
81

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
86

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 112
9 #include 2

18
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
98
[1,9,2,0,2,5]
50
18
00
[1,9,2,0,2,5]
106

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

18
8
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
111

18
8
[1,9,2,0,2,5]
113
Maximum Sub array product is 112
7

[1,9,2,0,2,5]
115
18
25

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

Maximum Sub array product is 112
3

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
18
1212
[1,9,2,0,2,5]
125#include 28

[1,9,2,0,2,5]
7

Is

Maximum Sub array product is 112
3

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
18
1212
[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 112
9
[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 112
3

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
18
1212____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

18
8
Maximum Sub array product is 112
39
18
3
[1,9,2,0,2,5]
159namespace53
[1,9,2,0,2,5]
179
[1,9,2,0,2,5]
195

18
8
[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]
159namespace53
[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]
159namespace53
[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

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
54
Maximum Sub array product is 112
39
18
3
[1,9,2,0,2,5]
159namespace53
[1,9,2,0,2,5]
179
[1,9,2,0,2,5]
232

18
8
[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

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
54

18
8
[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 112
99

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
18
33

[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]
159namespace53
[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
18
33

[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

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
39
18
3
[1,9,2,0,2,5]
171
[1,9,2,0,2,5]
131
[1,9,2,0,2,5]
165#include 92

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]
171
[1,9,2,0,2,5]
249
[1,9,2,0,2,5]
165
Maximum Sub array product is 112
99

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

Các

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
[1,9,2,0,2,5]
171
Maximum Sub array product is 112
99

Maximum Sub array product is 112
3

[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
18
3
Maximum Sub array product is 112
7#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
18
2
[1,9,2,0,2,5]
343

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
40

18
8
[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 112
44

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 112
46

[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 112
50

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
54
Maximum Sub array product is 112
39
Maximum Sub array product is 112
56

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 112
21

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 112
24

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
54
[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 112
70

[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 112
74

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
79

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 112
81

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
39
Maximum Sub array product is 112
86

18
8
Maximum Sub array product is 112
9 #include 2

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

Maximum Sub array product is 112
3

[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 112
01

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
03
Maximum Sub array product is 112
7
[1,9,2,0,2,5]
408

#include 35

[1,9,2,0,2,5]
410

Maximum Sub array product is 112
07

Đầ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 112
96

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
436

18
8
[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

18
8
Maximum Sub array product is 112
46

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

18
8
[1,9,2,0,2,5]
449

18
8
[1,9,2,0,2,5]
451

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
03
Maximum Sub array product is 112
7
[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 112
6
Maximum Sub array product is 112
7

Maximum Sub array product is 112
8
Maximum Sub array product is 112
9

& nbsp; giải pháp hiệu quả:

Maximum Sub array product is 112
3

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 112
3

[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 112
3

[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 112
96

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
436

18
8
[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

18
8
[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 112
3

18
8
[1,9,2,0,2,5]
451

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
03
Maximum Sub array product is 112
7
[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
18
3
Maximum Sub array product is 112
46
Maximum Sub array product is 112
47

Maximum Sub array product is 112
48
Maximum Sub array product is 112
49

& nbsp; giải pháp hiệu quả:

Maximum Sub array product is 112
3

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 112
96

[1,9,2,0,2,5]
8
18
2
18
3
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
436

18
8
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
439

18
8
[1,9,2,0,2,5]
451

Maximum Sub array product is 112
66
[1,9,2,0,2,5]
604

Maximum Sub array product is 112
66
[1,9,2,0,2,5]
449

Maximum Sub array product is 112
66
[1,9,2,0,2,5]
608

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

[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 112
03
Maximum Sub array product is 112
7
[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 112
46
[1,9,2,0,2,5]
646

[1,9,2,0,2,5]
570
Maximum Sub array product is 112
3

Maximum Sub array product is 112
3

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
18
2
18
39
18
40
18
41
18
3
[1,9,2,0,2,5]
80
18
55

18
8namespace08
18
33
18
59
18
3__

18
8using46
18
33 using79
18
3__

18
8using42
18
33
[1,9,2,0,2,5]
703

18
8using50
18
33
18
59
[1,9,2,0,2,5]
708

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9 namespace50

18
74
18
33
18
76
[1,9,2,0,2,5]
80
18
1212

18
95
18
33
18
97
18
98

18
99
[1,9,2,0,2,5]
738
[1,9,2,0,2,5]
739#include 92

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]
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
18
2
[1,9,2,0,2,5]
753

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

18
8
[1,9,2,0,2,5]
757

18
8
[1,9,2,0,2,5]
604

18
8
[1,9,2,0,2,5]
449

18
8
[1,9,2,0,2,5]
608

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

Maximum Sub array product is 112
3

[1,9,2,0,2,5]
770

[1,9,2,0,2,5]
771

Maximum Sub array product is 112
03
Maximum Sub array product is 112
7
[1,9,2,0,2,5]
774

Maximum Sub array product is 112
07

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

18
8
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
425

18
8
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
428

18
8
[1,9,2,0,2,5]
1
Maximum Sub array product is 112
96

18
8
18
2223
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
801

18
8
[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

18
8
Maximum Sub array product is 112
3

18
8
Maximum Sub array product is 112
9
Maximum Sub array product is 112
08

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
98
[1,9,2,0,2,5]
50
18
00
[1,9,2,0,2,5]
106

[1,9,2,0,2,5]
8
[1,9,2,0,2,5]
7

18
8
[1,9,2,0,2,5]
1
[1,9,2,0,2,5]
111

18
8
[1,9,2,0,2,5]
113
Maximum Sub array product is 112
7

[1,9,2,0,2,5]
214
18
25

[1,9,2,0,2,5]
8
Maximum Sub array product is 112
3

Maximum Sub array product is 112
3

Đầ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


Làm thế nào để tôi tìm thấy Subarray tiếp giáp lớn nhất trong Python?

Thuật toán cho chức năng Tìm..
Bước 1: Tạo một biến MAX_SUM và giá trị gán -1 cho nó. ....
Bước 2: Tạo một mảng biến, sẽ lưu trữ mảng phụ sẽ cho tổng tối đa ..
Bước 3: Lặp lại các phần tử của mảng bằng cách sử dụng chiều dài của mảng. ....
Bước 4: In Subarray đã cho số tiền tối đa ..

Làm thế nào để bạn tìm thấy Subarray tiếp giáp tối đa?

Cách tiếp cận đơn giản: Chạy vòng lặp cho I từ 0 đến n - 1, trong đó n là kích thước của mảng. Bây giờ, chúng tôi sẽ chạy một vòng lặp lồng cho J từ I đến N - 1 và thêm giá trị của phần tử tại Index J vào một biến thể biến đổi. Cuối cùng, đối với mỗi Subarray, chúng tôi sẽ kiểm tra xem CurrentMax có phải là tổng tối đa của tất cả các subarrays tiếp giáp không.

Làm thế nào để bạn tìm thấy sản phẩm tối đa của một mảng?

Đ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 bằng -6 và -2.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.

Làm thế nào để bạn tìm thấy sản phẩm tối đa của hai số trong danh sách Python?

Khoa học dữ liệu thực tế sử dụng Python..
N: = Kích thước của Nums ..
nums_sort: = Sắp xếp danh sách Nums ..
MAX_LEFT: = nums_sort [0] * nums_sort [1].
MAX_RIGHT: = nums_sort [n-1] * nums_sort [n-2].
Ans: = tối đa của max_left và max_right ..
trả lại ans ..