Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions KadanesAlgo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class KadanesAlgo
{
// Function to find the maximum sum of a contiguous subarray
// in a given integer array
public static int kadane(int[] A)
{
// stores the maximum sum subarray found so far
int maxSoFar = 0;

// stores the maximum sum of subarray ending at the current position
int maxEndingHere = 0;

// traverse the given array
for (int i: A)
{
// update the maximum sum of subarray "ending" at index `i` (by adding the
// current element to maximum sum ending at previous index `i-1`)
maxEndingHere = maxEndingHere + i;

// if the maximum sum is negative, set it to 0 (which represents
// an empty subarray)
maxEndingHere = Integer.max(maxEndingHere, 0);

// update the result if the current subarray sum is found to be greater
maxSoFar = Integer.max(maxSoFar, maxEndingHere);
}

return maxSoFar;
}

public static void main(String[] args)
{
int[] A = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };

System.out.println("The sum of contiguous subarray with the " +
"largest sum is " + kadane(A));
}
}