Sum of A1l Integers in a List (SUM) Input A list of integers Ala.. b] Output : s
ID: 3601224 • Letter: S
Question
Sum of A1l Integers in a List (SUM) Input A list of integers Ala.. b] Output : s--, A[i] Let S(Ala...b]) represent the output of the SUM problem on input Ala..b 4 points) 5. Statetwo different elf-reductions for the SUM problem. Usethe self-reduction examples from lecture as a guide. (4 points) 6. Give recursive algorithms based on your divide and conquer self-reductions to solve the SUM problem. (2 points) 7. What are the worst case runtimes of the solutions you have generated. (Just state the runtimes. You do not need to show your work.)Explanation / Answer
FindMaxSumSubArray(arr[],low,high){
//The array is empty
if (low > high)
return 0;
//only one element is present in the array
if (low == high)
return max(0, arr[low]);
//finding the middle element of the array
mid = (low + high) / 2;
// find maximum sum crossing to left
leftMax = sum = 0;
for (i = mid; i low; i--) {
sum += arr[i];
if (sum > leftMax)
leftMax = sum;
}
//find maximum sum crossing to right
rightMax = sum = 0;
for (i = mid+1; i high; i++) {
sum += arr[i];
if (sum > rightMax)
rightMax = sum;
}
// Return the maximum of leftMax, rightMax and their sum
return Math.max(leftMax + rightMax,
Math.max(FindMaxSumSubArray(low, mid), FindMaxSumSubArray(mid+1, high)));
}
The worst case time complexity of the algorithm is O(n log n).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.