I am looking forward to an answer to improve this code?
Thanks.
Test class
package test; import main.algorithms.LargestSumContiguousSubarray; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class LargestSumContiguousSubarrayTest { LargestSumContiguousSubarray largestSumContiguousSubarray; @Before public void setUp(){ largestSumContiguousSubarray = new LargestSumContiguousSubarray(); } @Test public void testSumContiguousSubArray(){ int[] a = {-2, -3, 4 - 1, -2, 1, 5, -3}; int sum = 7; Assert.assertEquals(sum, largestSumContiguousSubarray.kadenesAlgo(a)); } }
LargestSumContiguousSubarray.java class
package main.algorithms; public class LargestSumContiguousSubarray { // O(n) // Kadene's algorithm public int kadenesAlgo(int[] a) { // This is also works for negative numbers int max_so_far = a[0]; int curr_max = a[0]; for(int i=0;i<a.length; i++){ curr_max = Math.max(a[i], curr_max+a[i]); max_so_far = Math.max(max_so_far, curr_max); } return max_so_far; } }
, 4 - 1,
\$\endgroup\$