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.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1



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).

Add a comment
Know the answer?
Add Answer to:
Sum of A1l Integers in a List (SUM) Input A list of integers Ala.. b] Output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT