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
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
You are an algorithm designer and you came up with four different divide-and-conquer algorithms for some...
You are an algorithm designer and you came up with four different divide-and-conquer algorithms for some problem Q. Those four algorithms are described below in parts (1), (2), (3), and (4). You wrote those descriptions a long time ago so now you want to remind yourself, for each one of them, what was the corresponding recurrence relation and provide an upper bound on the running time. So first give the recurrence then solve it using any method of your choice...
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
In this problem your task is to find a missing number. The input will always consist...
In this problem your task is to find a missing number. The input will always consist of an array of n positive integers such that the difference between every two consecutive numbers is a fixed constant but one integer is missing. See below for two example inputs/outputs: Input sequence: [0, 2, 4, 6, 10] Output: missing number is 8 Input sequence: [1, 4, 7, 13, 16] Output: missing number is 10 Note that in the first example the constant c...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
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...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
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. 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