Skip to content

Commit

Permalink
Merge pull request #31 from uzairshaikh908/master
Browse files Browse the repository at this point in the history
i want to add kadane algorithm
  • Loading branch information
mridden authored Oct 21, 2021
2 parents add2014 + 280bef0 commit b4083dc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions java-program/kadane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.io.*;
// Java program to print largest contiguous array sum
import java.util.*;

class Kadane
{
public static void main (String[] args)
{
int [] a = {-2, -3, 4, -1, -2, 1, 5, -3};
System.out.println("Maximum contiguous sum is " +
maxSubArraySum(a));
}

static int maxSubArraySum(int a[])
{
int size = a.length;
int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;

for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
}

0 comments on commit b4083dc

Please sign in to comment.