Skip to content

Instantly share code, notes, and snippets.

@gingeleski
Last active September 20, 2016 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingeleski/171df4a19fecc5222ba82678bf6702b9 to your computer and use it in GitHub Desktop.
Save gingeleski/171df4a19fecc5222ba82678bf6702b9 to your computer and use it in GitHub Desktop.
Variant of Kadane's algorithm implemented in Java, using no Math calls
public class Main
{
public int maxSubArray(final List<Integer> a)
{
int ans = a.get(0);
int sum = a.get(0);
for (int i = 1; i < a.size(); i++)
{
if (sum + a.get(i) > a.get(i)) sum += a.get(i);
else sum = a.get(i);
ans = Math.max(ans, sum);
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment