algorithm - Finding minimal absolute sum of a subarray -


there's array a containing (positive , negative) integers. find (contiguous) subarray elements' absolute sum minimal, e.g.:

a = [2, -4, 6, -3, 9] |(−4) + 6 + (−3)| = 1 <- minimal absolute sum 

i've started implementing brute-force algorithm o(n^2) or o(n^3), though produced correct results. task specifies:

complexity: - expected worst-case time complexity o(n*log(n)) - expected worst-case space complexity o(n) 

after searching thought maybe kadane's algorithm can modified fit problem failed it.

my question - kadane's algorithm right way go? if not, point me in right direction (or name algorithm me here)? don't want ready-made code, need in finding right algorithm.

if compute partial sums such as

2, 2 +(-4), 2 + (-4) + 6, 2 + (-4) + 6 + (-3)... 

then sum of contiguous subarray difference of 2 of partial sums. find contiguous subarray absolute value minimal, suggest sort partial sums , find 2 values closest together, , use positions of these 2 partial sums in original sequence find start , end of sub-array smallest absolute value.

the expensive bit here sort, think runs in time o(n * log(n)).


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -