Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

find the time and space complexity of the following piece of java code: public static int [ ] findMaximumSubarray ( Vector A , int low,

find the time and space complexity of the following piece of java code:
public static int[] findMaximumSubarray(Vector A, int low, int high){
if (high == low){
return new int[]{low, high, A.get(low)};
} else {
int mid =(low + high)/2;
int[] left = findMaximumSubarray(A, low, mid);
int[] right = findMaximumSubarray(A, mid +1, high);
int[] cross = findMaxCrossingSubarray(A, low, mid, high);
if (left[2]>= right[2] && left[2]>= cross[2]){
return left;
} else if (right[2]>= left[2] && right[2]>= cross[2]){
return right;
} else {
return cross;
}
}
}
public static int[] findMaxCrossingSubarray(Vector A, int low, int mid, int high){
int leftSum = Integer.MIN_VALUE;
int sum =0;
int maxLeft =0;
for (int i = mid; i >= low; i--){
sum = sum + A.get(i);
if (sum > leftSum){
leftSum = sum;
maxLeft = i;
}
}
int rightSum = Integer.MIN_VALUE;
sum =0;
int maxRight =0;
for (int j = mid +1; j <= high; j++){
sum = sum + A.get(j);
if (sum > rightSum){
rightSum = sum;
maxRight = j;
}
}
return new int[]{maxLeft, maxRight, leftSum + rightSum};
}

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

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

Students also viewed these Databases questions

Question

Distinguish between poor and good positive and neutral messages.

Answered: 1 week ago

Question

Describe the four specific guidelines for using the direct plan.

Answered: 1 week ago