Question

Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...

Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.  

For example: the maximum subarray sum for the sequence [-2, 11, -4, 13, -5, 2] is 20. And the maximum subarray sum for [1, -3, 4, -2, -1, 6] is 7.

Of course you can easily design a O(n2) solution of this problem, we don’t want that here though.

Use your newly acquired knowledge of the Divide-and-Conquer algorithm design strategy to design a O(n log n) algorithm for this problem.

Following the classic divide-and-conquer steps noted in lecture 10 will be helpful in your task.

Please also note that we know this problem has a linear time solution using dynamic programming. Do not provide this solution as we will not get the chance to discuss it until next week.

For example:

Test Input Result
MaximumSubarraySum(arr);
-2 11 -4 13 -5 2
20
MaximumSubarraySum(arr);
1 -3 4 -2 -1 6
7

Homework Answers

Answer #1

Note: As You Haven't Provided any Specific Language and also you have not asked the solution in Divide And Conquer Technique or Dynamic Programming I am going to provide you both the techniques solutions. I have Choosen the Language as Java

Using Dynamic Programming(DP) Solution in Java:

import java.io.*;
import java.util.*;
public class SubArraySum
{
   static int maximumSumSubarray(int arr[], int n)
   {
       int min_prefix_sum = 0;
  
       int res = Integer.MIN_VALUE;
  
       int prefix_sum[] = new int[n];
       prefix_sum[0] = arr[0];
       for (int i = 1; i < n; i++)
           prefix_sum[i] = prefix_sum[i - 1]
                           + arr[i];     
  
       for (int i = 0; i < n; i++)
       {
           res = Math.max(res, prefix_sum[i] -
                       min_prefix_sum);
           min_prefix_sum = Math.min(min_prefix_sum,
                                   prefix_sum[i]);
       }
      
       return res;
   }
   public static void main (String[] args)
   {
       int arr1[] = { -2, 11, -4, 13, -5, 2 };
       int n1 = arr1.length;
       System.out.println(maximumSumSubarray(arr1, n1));
  
  
       int arr2[] = {1, -3, 4, -2, -1, 6 };
       int n2 = arr2.length;
       System.out.println(maximumSumSubarray(arr2, n2));
   }
}

Using Divide And Conquer Technique Solution in Java:

import java.io.*;
import java.util.*;

public class SubArraySum {
   static int maxSum(int arr[], int l,
                               int m, int h)
   {
       int sum = 0;
       int left_sum = Integer.MIN_VALUE;
       for (int i = m; i >= l; i--)
       {
           sum = sum + arr[i];
           if (sum > left_sum)
           left_sum = sum;
       }

       sum = 0;
       int right_sum = Integer.MIN_VALUE;
       for (int i = m + 1; i <= h; i++)
       {
           sum = sum + arr[i];
           if (sum > right_sum)
           right_sum = sum;
       }


       return Math.max(left_sum + right_sum, Math.max(left_sum, right_sum));
   }
   static int maxSubArraySum(int arr[], int l,
                                   int h)
   {
   if (l == h)
       return arr[l];
   int m = (l + h)/2;

   return Math.max(Math.max(maxSubArraySum(arr, l, m), maxSubArraySum(arr, m+1, h)), maxSum(arr, l, m, h));
   }

   public static void main(String[] args)
   {
   int arr[] = {-2, 11, -4, 13, -5, 2};
   int n = arr.length;
   int max_sum = maxSubArraySum(arr, 0, n-1);
   System.out.println(max_sum);
  
   int arr2[] = {1, -3, 4, -2, -1, 6 };
   int n2 = arr2.length;
   System.out.println(maxSubArraySum(arr2,0, n2-1));
   }
}

Know the answer?
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
Similar Questions
You're are working on n different projects, but in m hours you are going on vacation....
You're are working on n different projects, but in m hours you are going on vacation. Imagine that for each project i, you had a function fi that told you how happy the people paying for project i would be if out your m available hours you devoted 0 ≤ x ≤ m to project i. Imagine further that each such function has maximum value at most s, corresponding to the project being fully finished (and thus the clients being...
QUESTION 1 1. Brianna is trying to increase her chances of being promoted to vice president...
QUESTION 1 1. Brianna is trying to increase her chances of being promoted to vice president by working to build good work relationships with other managers outside her own department. Brianna's behavior should be viewed as dysfunctional politics. functional politics. coercive power. functional influence. 2 points QUESTION 2 1. The Gingerbread Factory has a separate unit that makes their chocolate crunch cookies and another unit that is completely responsible for all operations in producing their ginger snap cookies. The Gingerbread...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
1) Describe an example of each of the following that may be found of your kitchen:...
1) Describe an example of each of the following that may be found of your kitchen: Explain how your choice falls into this category, and if there is a chemical name or symbol for it, provide that as well. Provide a photo of your example with your ID card in it. a) a compound b) a heterogeneous mixture c) an element (symbol) Moving to the Caves… Lechuguilla Caves specifically. Check out this picture of crystals of gypsum left behind in...
1. The failure of the new supply chain system affected Nike adversely. What were the reasons...
1. The failure of the new supply chain system affected Nike adversely. What were the reasons for the failure and how did the breakdown harm Nike? 2. What are the important elements to be kept in mind while implementing a new system in an organization? What is the importance of a good working relationship between partners and the sharing of responsibility in implementing critical projects? What mistakes did Nike and i2 make? 3. comment on the lessons learned and the...
      MK Restaurant: Branding of Thai-Style Hotpot The restaurant industry is one of the most...
      MK Restaurant: Branding of Thai-Style Hotpot The restaurant industry is one of the most competitive in Thailand. With a large number of players ranging from restaurants in five-star hotels, global fast-food chains to small stalls along the streets and everything in between, the Thais are spoiled for choice. In addition, as the world becomes globalized, consumers are familiar with international dishes and would not hesitate to try new offerings from the other side of the globe. As a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Consider the function ?(?) = sin(?). Find the Taylor series formula when centered at ? =...
    asked 2 minutes ago
  • Calculate the force in the cable CD is the weight of the traffic light is 20...
    asked 10 minutes ago
  • research study: Impact of Variation Order in Construction Project Question: give me some specific objective (inquiries...
    asked 13 minutes ago
  • The Bureau of Labor Statistics’ American Time Use Survey showed that the amount of time spent...
    asked 19 minutes ago
  • North Company has a fiscal year ending on December 31. Its financial statements for the years...
    asked 20 minutes ago
  • a)Can you evaluate the work done by the field F=Mi+Nj+Pk over a 2d region (such as...
    asked 27 minutes ago
  • A courier service company wishes to estimate the proportion of people in various states that will...
    asked 29 minutes ago
  • Please show work Find the minimum diameter of an l = 19.4 m long copper wire...
    asked 33 minutes ago
  • Magnetic surveying is one technique used by archaeologists to determine anomalies arising from variations in magnetic...
    asked 38 minutes ago
  • The following multiple regression printout can be used to predict a person's height (in inches) given...
    asked 1 hour ago
  • The welfare loss from monopoly can be seen from the difference in equilibrium between a. marginal...
    asked 1 hour ago
  • 1.   Calculate the sanitary flow for a 9,000 sf non-medical office building with       Daily flow____________________________________________________________...
    asked 1 hour ago