Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Let A be an array of integers ( positive or negative ) . Assume that the array is of size n . The subarray A

Let A be an array of integers (positive or negative). Assume that the array is of size n. The subarray A[i..j] is the part of the array that starts at index i and ends at index j, where 0ijn-1. Let sij equal the sum of the integers in A[i.j].
We wish to solve the following problem:
Important: Find the maximum value for sij over all subarrays with a maximum length of 3 in array A, where 0ijn-1.
The three algorithms given below solve this problem. NOTE: If all of the values in the array are negative, then the maximum value for sij is O by default.
Example: If the array contains the values {-1,12,-3,14,-4,3}, then the maximum sum over all subarrays is 23(for the subarray {12,-3,14}). If the array contains the values {2,-3,5,-1,7}, then the maximum sum over all subarrays is 11(for the subarray {5,-1,7}).
You are to write a Java program that determines the amount of work each of these algorithms does to compute its answer for arrays of various sizes. Using this data, you are to determine the runtime complexity of each algorithm.
ALGORITHM 1
Start with a maximum sum of 0. Compute the sum of each 1-element subarray, then compute the sum of each 2 element subarray, then compute the sum of each 3-element subarray, etc. For each sum you compute, if it is larger than the maximum sum you've seen, then it becomes the maximum sum.
ALGORITHM 2
Same as algorithm 1 but now once you compute the sum of the subarray from A[i] to A[j], the sum of the subarray from A[i] to A[j+1] is just the previous sum you computed plus A[j+1]. Don't add up all of the previous values all over again.
ALGORITHM 3(Optional)
Same as algorithm 1 but store the temporary steps in an array.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions

Question

25.0 m C B A 52.0 m 65.0 m

Answered: 1 week ago