# Homework: 2D Array Max Subarray Sum - Need Help

I used a nested for loops that adds array[i][j] to the a currentSum variable. And after that is a conditional statement that give sum the currentSum value if sum < currentSum and then return the sum. This did not work for arrays with negative sums.

I’ve tried to give sum the lowest array[i][j] value but did not work.

Hey Dbug,

Currently, when the max sum is less than 0, your submission does not work because you initialize your sum variable as 0, and this variable’s value only changes when currentSum > sum.

As the question information suggests, try to initialize sum as the sum of the first row. You can do this by calculating the currentSum of the first row (the logic for which you already seem to have) and storing it as sum. Now that sum is initialized to the sum of a row in the given 2d array, revert to the same logic that you describe in your post. You can perform this by using some boolean variable to keep track of whether or not the sum variable has been initialized as the sum of the first row.

From the question information itself:
One hint for this problem is that you may need both an int variable to store the max and a boolean variable to record whether the maximum value has been initialized. Once you have summed each subarray, check whether either your boolean value is false or the sum is larger than the largest you’ve seen so far. After you check the sum of the first subarray, set your boolean value to true.

Hope this helps!

2 Likes