Kadane's algorithm is a dynamic programming approach to the maximum subarray problem, that is, is the task of finding the contiguous subarray within a one-dimensional array of numbers (containing at least one positive number) which has the largest sum.
Questions tagged [kadanes-algorithm]
80 questions
40
votes
15 answers
Maximum subarray sum modulo M
Most of us are familiar with the maximum sum subarray problem. I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M.
The naive approach to solve this variant would be to…

Bhoot
- 2,614
- 1
- 19
- 36
25
votes
2 answers
Dynamic programming aspect in Kadane's algorithm
Initialize:
max_so_far = 0
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0
(c) if(max_so_far < max_ending_here)
…

Bhavish Agarwal
- 663
- 7
- 13
24
votes
18 answers
Kadane Algorithm Negative Numbers
int array[] = {-1, 4, -2, 5, -5, 2, -20, 6};
If I had that array, my Kadane algorithm implementation to find the maximum subarray works:
int max_so_far = INT_MIN;
int max_ending_here = 0;
for (int i = 0; i < size; i++) {
max_ending_here =…

David Gomes
- 5,644
- 16
- 60
- 103
19
votes
6 answers
Kadane's algorithm explained
Could someone take me through what is happening here in Kadane's algorithm? Wanted to check my understanding. here's how I see it.
you are looping through the array, and each time you set the ans variable to the largest value seen, until that…

devdropper87
- 4,025
- 11
- 44
- 70
14
votes
11 answers
Finding minimal absolute sum of a subarray
There's an array A containing (positive and negative) integers. Find a (contiguous) subarray whose elements' absolute sum is minimal, e.g.:
A = [2, -4, 6, -3, 9]
|(−4) + 6 + (−3)| = 1 <- minimal absolute sum
I've started by implementing a…

NPS
- 6,003
- 11
- 53
- 90
13
votes
7 answers
largest sum of contiguous subarray No Larger than k
For example,
we have
{2,2,-1},
when k = 0, return -1.
when k = 3, return 3.
This is even tricky because we have negative numbers and an additional variable k. k can be any value, negative, don't make any assumption.
I cannot refer to…

Jerry Z.
- 2,031
- 3
- 22
- 28
11
votes
3 answers
Kadane's Algorithm in Scala
Does anyone have a Scala implementation of Kadane's algorithm done in a functional style?
Edit Note: The definition on the link has changed in a way that invalidated answers to this question -- which goes to show why questions (and answers) should…

S0rin
- 1,283
- 1
- 10
- 22
9
votes
4 answers
kadane algorithm in java
I have the following implementation of Kadane's algorithm in java. It is basically to find the maximum sum of contiguous subarray.
String[] numbers = string.split(",");
int max_so_far = 0;
int max_ending_here = 0;
…

aherlambang
- 14,290
- 50
- 150
- 253
8
votes
6 answers
Kadane's algorithm to find subarray with the maximum sum
I have the following implementation of Kadane's algorithm to solve the problem of the maximum subarray of an array:
public static decimal FindBestSubsequence
(this IEnumerable source, out int startIndex, out int endIndex)
{
decimal…

Ignacio Soler Garcia
- 21,122
- 31
- 128
- 207
8
votes
9 answers
How to return maximum sub array in Kadane's algorithm?
public class Kadane {
double maxSubarray(double[] a) {
double max_so_far = 0;
double max_ending_here = 0;
for(int i = 0; i < a.length; i++) {
max_ending_here = Math.max(0, max_ending_here + a[i]);
max_so_far =…

Aqib Saeed
- 115
- 1
- 2
- 11
7
votes
12 answers
Find max sum of elements in an array ( with twist)
Given a array with +ve and -ve integer , find the maximum sum such that you are not allowed to skip 2 contiguous elements ( i.e you have to select at least one of them to move forward).
eg :-
10 , 20 , 30, -10 , -50 , 40 , -50, -1, -3
Output :…

user1057741
- 265
- 1
- 2
- 10
6
votes
2 answers
Understanding what's happening in the Kadane Algorithm (Python)
I'm having a difficult time understanding what's happening in these two examples I found of the Kadane Algorithm. I'm new to Python and I'm hoping understanding this complex algo will help me see/read programs better.
Why would one example be…

William Zebrowski
- 233
- 2
- 13
6
votes
5 answers
Codility - min average slice
I'm trying to find a solution to a codility question on minimum slice of a subarray, and I've devised a solution using a modified version of Kadane's algorithm. I've currently gotten 90/100 and managed to pass almost all the tests in O(n). However,…

Dan Tang
- 1,273
- 2
- 20
- 35
6
votes
3 answers
Understanding Kadane's Algorithm for 2-D Array
I'm trying to write a program which solves the maximum subarray problem. I can understand the intuition behind Kadane's Algorithm on a 1-D array as well as the O(N^4) implementation on a 2-D array. However, I am having some trouble understanding the…

boxme
- 187
- 1
- 6
- 14
5
votes
2 answers
Maximum subarray problem - min value solution?
Have you ever felt like your head wasn't meant for an algorithm?
I tried solving the maximum subarray problem and I came across this solution on Codewars:
var maxSequence = function(arr){
var min = 0, ans = 0, i, sum = 0;
for (i = 0; i <…

Xen_mar
- 8,330
- 11
- 51
- 74