Hướng dẫn contiguous subsequences in python - dãy con liền kề trong python

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:44 (UTC/GMT +8 giờ)

Python Basic - 1: Tập thể dục -44 với giải pháp

Viết một chương trình Python để tìm tổng tối đa của một chuỗi tiếp giáp từ một chuỗi số nhất định của các số A1, A2, A3, ... An. Một phần sau của một yếu tố cũng là một phần sau liên tục.

Nội phân chính

  • Python Basic - 1: Tập thể dục -44 với giải pháp
  • Viết một chương trình Python để tìm tổng tối đa của một chuỗi tiếp giáp từ một chuỗi số nhất định của các số A1, A2, A3, ... An. Một phần sau của một yếu tố cũng là một phần sau liên tục.
  • Nội phân chính
  • Python: Lời khuyên trong ngày
  • Làm thế nào để tôi tìm thấy Subarray tiếp giáp lớn nhất trong Python?
  • Trình tự tiếp giáp là gì?

Trình tự tiếp giáp trong mảng là gì?
You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.
Input numbers are separated by a space.
Input 0 to exit.
Input number of sequence of numbers you want to input (0 to exit): 3
Input numbers:
2
4
6
Maximum sum of the said contiguous subsequence:
12 Input number of sequence of numbers you want to input (0 to exit): 0

Làm thế nào để tôi tìm thấy Subarrays trong Python?:

Đầu vào: Bạn có thể giả sử rằng 1 ≤ N ≤ 5000 và -100000 ≤ AI ≤ 100000. Số đầu vào được phân tách bằng một không gian. Đầu vào 0 để thoát. Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 3 Số đầu vào: 2 4 6 Tổng số tối đa của chuỗi con tiếp giáp: 12 Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 0

while True:
    print("Input number of sequence of numbers you want to input (0 to exit):")
    n = int(input())
    if n == 0:
        break
    else:
        A = []
        Sum = []
        print("Input numbers:") 
        for i in range(n):
            A.append(int(input()))
        Wa = 0
        for i in range(0,n):
            Wa += A[i]
            Sum.append(Wa)
        for i in range(0 , n):
            for j in range(0 , i):
                Num = Sum[i] - Sum[j]
                Sum.append(Num)
        print("Maximum sum of the said contiguous subsequence:")
        print(max(Sum))

Giải pháp mẫu:

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0

Flowchart:

Hướng dẫn contiguous subsequences in python - dãy con liền kề trong python

Mã Python:

Đầu ra mẫu:

Trình chỉnh sửa mã Python:Write a Python program to test whether two lines PQ and RS are parallel. The four points are P(x1, y1), Q(x2, y2), R(x3, y3), S(x4, y4).
Next: Write a python program to test if circumference of two circles intersect or overlap.

Viết một chương trình Python để tìm tổng tối đa của một chuỗi tiếp giáp từ một chuỗi số nhất định của các số A1, A2, A3, ... An. Một phần sau của một yếu tố cũng là một phần sau liên tục.

Nội phân chính

>>> range(0,10,2)
[0, 2, 4, 6, 8]  

Python: Lời khuyên trong ngày

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

Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far

Trình tự tiếp giáp là gì? 
The simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive-sum compare it with max_so_far and update max_so_far if it is greater than max_so_far 

    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4

Program:  

Python3

Trình tự tiếp giáp trong mảng là gì?

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
2
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
1

Làm thế nào để tôi tìm thấy Subarrays trong Python?

Đầu vào: Bạn có thể giả sử rằng 1 ≤ N ≤ 5000 và -100000 ≤ AI ≤ 100000. Số đầu vào được phân tách bằng một không gian. Đầu vào 0 để thoát. Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 3 Số đầu vào: 2 4 6 Tổng số tối đa của chuỗi con tiếp giáp: 12 Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 0

Giải pháp mẫu:

Mã Python:

Đầu ra mẫu:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước đây: Viết chương trình Python để kiểm tra xem hai dòng PQ và RS có song song hay không. Bốn điểm là P (X1, Y1), Q (X2, Y2), R (X3, Y3), S (X4, Y4).

Tạo một chuỗi các số (từ 0 đến mười với bỏ qua):

Viết một chương trình hiệu quả để tìm tổng số subarray liên tục trong một mảng một chiều các số có số tiền lớn nhất. & NBSP;

Thuật toán Kadane từ:

Giải thích: & nbsp; Ý tưởng đơn giản của thuật toán Kadane, là tìm kiếm tất cả các phân đoạn tiếp giáp tích cực của mảng (MAX_IVED_HERE được sử dụng cho việc này). Và theo dõi phân đoạn liên tục tổng tối đa trong số tất cả các phân đoạn dương (MAX_SO_FAR được sử dụng cho việc này). Mỗi lần chúng tôi nhận được một tổng dương so sánh nó với MAX_SO_FAR và cập nhật MAX_SO_FAR nếu nó lớn hơn MAX_SO_FAR & NBSP;

Output:

Maximum contiguous sum is 7

from math

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
0
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
1

Python3

Làm thế nào để tôi tìm thấy Subarrays trong Python?

Đầu vào: Bạn có thể giả sử rằng 1 ≤ N ≤ 5000 và -100000 ≤ AI ≤ 100000. Số đầu vào được phân tách bằng một không gian. Đầu vào 0 để thoát. Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 3 Số đầu vào: 2 4 6 Tổng số tối đa của chuỗi con tiếp giáp: 12 Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 0

Giải pháp mẫu:

Mã Python:

Đầu ra mẫu:

Trước đây: Viết chương trình Python để kiểm tra xem hai dòng PQ và RS có song song hay không. Bốn điểm là P (X1, Y1), Q (X2, Y2), R (X3, Y3), S (X4, Y4).

Tạo một chuỗi các số (từ 0 đến mười với bỏ qua):

Viết một chương trình hiệu quả để tìm tổng số subarray liên tục trong một mảng một chiều các số có số tiền lớn nhất. & NBSP;

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Viết một chương trình hiệu quả để tìm tổng số subarray liên tục trong một mảng một chiều các số có số tiền lớn nhất. & NBSP;

Thuật toán Kadane từ:O(n) 

Giải thích: & nbsp; Ý tưởng đơn giản của thuật toán Kadane, là tìm kiếm tất cả các phân đoạn tiếp giáp tích cực của mảng (MAX_IVED_HERE được sử dụng cho việc này). Và theo dõi phân đoạn liên tục tổng tối đa trong số tất cả các phân đoạn dương (MAX_SO_FAR được sử dụng cho việc này). Mỗi lần chúng tôi nhận được một tổng dương so sánh nó với MAX_SO_FAR và cập nhật MAX_SO_FAR nếu nó lớn hơn MAX_SO_FAR & NBSP;Dynamic Programming
Following is another simple implementation suggested by Mohit Kumar. The implementation handles the case when all numbers in the array are negative. 

Python3

Làm thế nào để tôi tìm thấy Subarrays trong Python?

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
8
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
14
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
02

Đầu vào: Bạn có thể giả sử rằng 1 ≤ N ≤ 5000 và -100000 ≤ AI ≤ 100000. Số đầu vào được phân tách bằng một không gian. Đầu vào 0 để thoát. Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 3 Số đầu vào: 2 4 6 Tổng số tối đa của chuỗi con tiếp giáp: 12 Số đầu vào của chuỗi số bạn muốn nhập (0 để thoát): 0

Giải pháp mẫu:

Mã Python:

Đầu ra mẫu:

Viết một chương trình hiệu quả để tìm tổng số subarray liên tục trong một mảng một chiều các số có số tiền lớn nhất. & NBSP;

Thuật toán Kadane từ:

Giải thích: & nbsp; Ý tưởng đơn giản của thuật toán Kadane, là tìm kiếm tất cả các phân đoạn tiếp giáp tích cực của mảng (MAX_IVED_HERE được sử dụng cho việc này). Và theo dõi phân đoạn liên tục tổng tối đa trong số tất cả các phân đoạn dương (MAX_SO_FAR được sử dụng cho việc này). Mỗi lần chúng tôi nhận được một tổng dương so sánh nó với MAX_SO_FAR và cập nhật MAX_SO_FAR nếu nó lớn hơn MAX_SO_FAR & NBSP;

Output: 

Maximum contiguous sum is 7

from math

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
0
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
1

Python3

from

>>> range(0,10,2)
[0, 2, 4, 6, 8]  
22
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
0
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
24

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
5
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
6

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
8
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
0
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
31
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
0
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
3

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
5
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
39
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
43
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
47
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
9
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
0
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
1
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
2
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
75

Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
6
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
5
    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
0
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
1

Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
6
    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
65

    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
5
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
8
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
8

    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
5
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
39
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
73

    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
5
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
43
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
77

Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
6
    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
3
Maximum contiguous sum is 7
1
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7
Maximum contiguous sum is 7
3

    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
5
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
5
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
7

    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
5
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
47
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
77
    Lets take the example:
    {-2, -3, 4, -1, -2, 1, 5, -3}

    max_so_far = max_ending_here = 0

    for i=0,  a[0] =  -2
    max_ending_here = max_ending_here + (-2)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=1,  a[1] =  -3
    max_ending_here = max_ending_here + (-3)
    Set max_ending_here = 0 because max_ending_here < 0

    for i=2,  a[2] =  4
    max_ending_here = max_ending_here + (4)
    max_ending_here = 4
    max_so_far is updated to 4 because max_ending_here greater 
    than max_so_far which was 0 till now

    for i=3,  a[3] =  -1
    max_ending_here = max_ending_here + (-1)
    max_ending_here = 3

    for i=4,  a[4] =  -2
    max_ending_here = max_ending_here + (-2)
    max_ending_here = 1

    for i=5,  a[5] =  1
    max_ending_here = max_ending_here + (1)
    max_ending_here = 2

    for i=6,  a[6] =  5
    max_ending_here = max_ending_here + (5)
    max_ending_here = 7
    max_so_far is updated to 7 because max_ending_here is 
    greater than max_so_far

    for i=7,  a[7] =  -3
    max_ending_here = max_ending_here + (-3)
    max_ending_here = 4
0
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
3

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
03
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
96
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
97
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
98

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
03
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
3
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
0229297
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
04

Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
7
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
03
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
3
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
08
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
97
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
10

Maximum contiguous sum is 7
1
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
3
Maximum contiguous sum is 7
3
>>> range(0,10,2)
[0, 2, 4, 6, 8]  
0
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
95
Maximum contiguous sum is 7
.

Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
35
Input number of sequence of numbers you want to input (0 to exit):
 3
Input numbers:
 2
 4
 6
Maximum sum of the said contiguous subsequence:
12
Input number of sequence of numbers you want to input (0 to exit):
 0
07
Initialize:
    max_so_far = INT_MIN
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
  (c) if(max_ending_here < 0)
            max_ending_here = 0
return max_so_far
37

Output: 

Maximum contiguous sum is 7
Starting index 2
Ending index 6

Thuật toán Kadane sườn có thể được xem cả như một người tham lam và DP. Như chúng ta có thể thấy rằng chúng ta đang giữ một tổng số số nguyên và khi nó trở nên nhỏ hơn 0, chúng ta đặt lại thành 0 (phần tham lam). Điều này là do việc tiếp tục với một khoản tiền âm kém hơn so với khởi động lại với phạm vi mới. Bây giờ nó cũng có thể được xem dưới dạng DP, ở mỗi giai đoạn chúng tôi có 2 lựa chọn: lấy phần tử hiện tại và tiếp tục với tổng trước hoặc khởi động lại một phạm vi mới. Cả hai lựa chọn này đang được chăm sóc trong việc thực hiện. & NBSP;

Độ phức tạp về thời gian: O (n)O(n)

Không gian phụ trợ: O (1)O(1)

Bây giờ hãy thử câu hỏi dưới đây & nbsp; đưa ra một loạt các số nguyên (có thể là một số yếu tố âm), hãy viết một chương trình C để tìm ra * sản phẩm tối đa * có thể bằng cách nhân các số nguyên liên tiếp trong mảng trong đó n ≤ mảng_size. Ngoài ra, in điểm bắt đầu của sản phẩm tối đa Subarray.
Given an array of integers (possibly some elements negative), write a C program to find out the *maximum product* possible by multiplying ‘n’ consecutive integers in the array where n ≤ ARRAY_SIZE. Also, print the starting point of the maximum product subarray.

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ên.


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

Trình tự tiếp giáp là gì?

Sau đó, nó sẽ được gọi là IFF liền kề Các yếu tố được thực hiện theo thứ tự liên tiếp trong bộ gốc. Ví dụ, trình tự = 2,3, ABC, 5,6,4, Abhishek; Sau = 5,6,2, Abhishek; Trại sau liền kề = 3, ABC, 5.6 hoặc 5,6,4, Abhishek hoặc ABC, 5.6.iff the elements taken in order are consecutive in original set. E.g, Sequence=2,3,abc,5.6,4,abhishek; Subsequence=5.6,2,abhishek; Contiguous Subsequence=3,abc,5.6 or 5.6,4,abhishek or abc,5.6.

Trình tự tiếp giáp trong mảng là gì?

Một subarray liền kề chỉ đơn giản là một subarray của một mảng có điều kiện là các phần tử của subarray phải theo trình tự chính xác như là chuỗi của các phần tử trong mảng.a subarray of an array with a condition that the elements of the subarray should be in exact sequence as the sequence of the elements in the array.

Làm thế nào để tôi tìm thấy Subarrays trong Python?

Cách tiếp cận số 1: Để có được Subarray, chúng ta có thể sử dụng cắt lát để lấy Subarray. Bước 1: Chạy vòng lặp cho đến chiều dài+1 của danh sách đã cho. Bước 2: Chạy một vòng lặp khác từ 0 đến i. Bước 3: Cắt lát Subarray từ J đến i.use slicing to get the subarray. Step 1: Run a loop till length+1 of the given list. Step 2: Run another loop from 0 to i. Step 3: Slice the subarray from j to i.