You are viewing a single comment's thread. Return to all comments →
Java solution
public static List<Integer> maxSubarray(List<Integer> arr) { int maxSubArraySum = 0; int maxSubSequenceSum = 0; int subArraySum = 0; int n = arr.size(); List<Integer> copyArray = new ArrayList<>(arr); Collections.sort(copyArray); if(copyArray.get(n-1) <0){ return Arrays.asList(copyArray.get(n-1),copyArray.get(n-1)); } for(Integer el : arr){ if(el>=0) maxSubSequenceSum +=el; subArraySum+=el; if(subArraySum<0){ subArraySum = 0; } maxSubArraySum = Math.max(maxSubArraySum, subArraySum); } return Arrays.asList(maxSubArraySum, maxSubSequenceSum); }
Seems like cookies are disabled on this browser, please enable them to open this website
The Maximum Subarray
You are viewing a single comment's thread. Return to all comments →
Java solution