Maximum sum of contiguous sub-sequence with length at most k

rockmyboat

I am trying to modify the Kadane Algorithm in order to solve a more specific problem.

def max_Sum(arr, length, k):
if length < k:
    print('length of array should be greater than k')

res = 0
for i in range(0, k):
    res += arr[i]

current_sum = res

for i in range(k, n):
    if k == n:
        for j in range(0, k-1):
            res += arr[j]
        current_sum = res
    else:
        current_sum += arr[i] - arr[i-k]
        print(res, current_sum)
        res = max(res, current_sum)

return res

This is the code for the maximum subarray problem. What I want to do is find the maximum subarray with length at most K.

Example: We have an array A = [3,-5 1 2,-1 4,-3 1,-2] and we want to find the maximum subarray of length at most K = 9. Length of subarray should not be restricted at K, if there is another length L < K that provides a greater sum, the algorithm should return sum[:L].

In this case, the algorithm will return 0. It should return 6, following the sum of A[2:5].

Francisco Geiman

Well, a solution that works in O(n * K) is to use sliding windows for every possible length <= K. I have tried to find a O(n) correct solution modifying Kadane, but I couldn't.

def best_array_fixed_k( arr, length, k ):
    total_sum = 0
    best = 0
    for x in xrange( 0, length ):
        total_sum = total_sum + arr[x]
        if x >= k:
            total_sum = total_sum - arr[x - k]
        if x >= k - 1:
            best = max( best, total_sum )
            # this makes sure that we are considering a window with length = k
    return best

def max_sum( arr, length, k):
    best = 0
    for x in xrange( 1, k + 1):
        best = max( best, best_array_for_fixed_k(arr, length, x ) )
    return best

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find the maximum sum of all contiguous subarrays and not consecutive each others which its length at most 'k' in a given array

Finding a maximum sum contiguous sub array

Dynamic programming - maximum sum increasing sub sequence

sum of maximum element of sliding window of length K

What is the algorithm to find the maximum sum of an array sub-sequence?

Maximum sum subarray with the length smaller than k problem . O(n)

Maximum difference in contiguous, fixed-length subsequence

Maximum contiguous sum using Divide and Conquer

Maximum sum of any contiguous subarray of the array

largest sum of contiguous subarray No Larger than k

Possibly simpler O(n) solution to find the Sub-array of length K (or more) with the maximum average

Maximum Sub-Set Sum

To Find The Maximum sum of sub array

Maximum Sum of elements of array which is not divisible by M, deleting/avoiding at most K consequtive array elements

Number of sub sequences of length K having total sum S, given 2d array

Optimize: Divide an array into continuous subsequences of length no greater than k such that sum of maximum value of each subsequence is minimum

Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

Find contiguous subsequences such that cube of the sum of numbers is at most y

Counting all contiguous sub-arrays given sum zero

Maximum sum in a contiguous subarray of a given 2D array

How to know a generated sequence is at most a certain length

Calculate Run Length Sequence and Maximum by Subject ID

What's the maximum length for a multibyte escape sequence?

JavaScript: return all contiguous subarrays whose sum equals K

Interview question maximum subarray of length k

Maximum sum of subsequence of length L with a restriction

Finding 2 equal sum sub-sequences, with maximum sum?

Maximum sum of k connected elements of a matrix

Maximum value of sum of K partitioned subarrays