Question

Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two ind

need help in this algorithm question

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

In dirides and conquer we b«eak the same type maxumuumcan occux ii one the 5 nup Case3 없bit ltad past and.erdaự second part C

Algorithm:

Function 1 (MaximumSubArraySum):

/*

This function will return a set of three values (i,j,sum)

i = starting index of maximum sum subarray

j = ending index of the maximum sum subarray

sum = sum of all elements from index i to j in the array A

*/

/*

To call the MaximumSubArraySum function we will give:

start=0 and end=A.size()-1

Therefore in the main function we will write,

MaximumSubArraySum(A,0,A.size()-1)

*/

MaximumSubArraySum(A,start,end)

/*

Base Case:

Only 1 element is present in the array

Hence we will return (start, end, sum)

and sum is A[start] as only single elements is there

*/

if (end == start)

{

return (start, end, A[start])

}

else

{

/*

Find the middle index

*/

mid = floor( (start + end)/2 )

/*

Case 1:

Recursive call on the left/first part of the array A

The left part will start from start and end at mid index

Here,

ls = starting index of max sum subarray in left part

le = ending index of max sum subarray in left part

LeftSum = sum from A[ls] to A[le]

*/

(ls,le,LeftSum) = MaximumSubArraySum(A,start,mid)

/*

Case 2:

Recursive call on the right/second part of the array A

The left part will start from mid+1 index and end at end index

Here,

rs = starting index of max sum subarray in right part

re = ending index of max sum subarray in right part

RightSum = sum from A[rs] to A[re]

*/

(rs,re,RightSum) = MaximumSubArraySum(A,mid+1,end)

/*

Case 3:

Here we calculate the sum starting in the first half of the array

and ending in the second half of the array

Here,

ms = starting index of max sum subarray in left part

me = ending index of max sum subarray in right part

MiddleSum = sum from A[ms] to A[me]

*/

(ms,me,MiddleSum) = MaxXingSubarray(A,start,mid,end)

/*

Find the maximum of the sums returned from all the three cases

*/

// If the LeftSum is greater than both MiddleSum and RightSum

if LeftSum >= MiddleSum and LeftSum >= RightSum

return (ls,le,LeftSum)

// If the RightSum is greater than both MiddleSum and LeftSum

else if RightSum >= MiddleSum and RightSum >= LeftSum

return (rs,re,RightSum)

// Else the MiddleSum is greater than both LeftSum and RightSum

else

return (ms,me,MiddleSum)

}

}

Function 2 ( Called in Function 1 ):

FindMaxSum(A,start,middle,end)

/*

Starting from the middle element we will go down to the start index

by decresing i by one everytime and going till we get a sum which is greater

than the previous sum otherwise we will stop

*/

LeftSum = -infinity;

sum = 0;

for i = middle downto start {

sum = sum + A[i]

if sum > LeftSum {

LeftSum = sum

startingIndex = i

}

}

/*

Starting from the middle element we will go up to the end index

by increasing i by one everytime and going till we get a sum which is greater

than the previous sum otherwise we will stop

*/

RightSum = -infinity;

sum = 0;

for j = middle+1 to end {

sum = sum + A[j]

if sum > RightSum

{

RightSum = sum

endingIndex = j

}

}

/*

starting index is the maximum we can go to the left

ending index is the maximum we can go to the right

*/

finalSum = LeftSum + RightSum

return (startingIndex,endingIndex,finalSum)

Time Complexity:

case cost& 1 Recunence selalion 2 2. 0​​​​​​​

fin)-n Case 2, Tune - Olnlbqn)

Add a comment
Know the answer?
Add Answer to:
Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...
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
  • (13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conqu...

    (13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...

  • Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a...

    Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing and combining steps take a time in Θ(n n). Write a recurrence equation for the running time T (n) , and solve the equation for T (n) 2. Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing...

  • 1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct...

    1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.

  • Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j...

    Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...

  • An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1≤i<k≤m and ...

    An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1≤i<k≤m and 1≤j<l≤n , we have >A[i,j]+a[k,l]≤A[i,l]+A[k,j]> In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and columns, the sum of the upper-left and lower-right elements is less than or equal to the sum of the lower-left and upper-right elements. For example, the following...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • My homework task is: Let a linear, binary, array A and two positives real numbers a and b, with a < b. Give a dynamic...

    My homework task is: Let a linear, binary, array A and two positives real numbers a and b, with a < b. Give a dynamic programming algorithm using bottom-up approach, that splits table A to the minimum numbers of continuous sub-arrays, so that the percentage of units (1s) <= a, OR percentage of units (1s) >= b.. Analyze the run time of the algorithm. For example, with input A = [1; 0; 1; 1; 1; 0; 0; 1; 0; 0;...

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