Question: / * Start with a maximum sum of 0 . Compute the sum of each 1 - element subarray, then compute the sum of each
Start with a maximum sum of Compute the sum of each element subarray,
then compute the sum of each element subarray,
then compute the sum of each element subarray, etc. For each sum you compute,
if it is larger than the maximum sum you've seen, then it becomes the maximum sum.
public static int algorithmint nums
int maxSum ;
forint i;imaxSum
maxSum numsi;
forint i;imaxSum
maxSum newSum;
forint i;imaxSum
maxSum newSum;
return maxSum;
Same as algorithm but now once you compute the sum of the subarray from Ai to Aj
the sum of the subarray from Ai to Aj is just the previous sum you computed plus Aj
Don't add up all of the previous values all over again.
public static int algorithmint nums
int maxSum ;
int n nums.length;
for int start ; start n; start
int currentSum ;
for int end start; end n; end
currentSum numsend;
if currentSum maxSum
maxSum currentSum;
is this rigfht is there anything that i can do to make it better
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
