Question: can you make a java method that gets the optimal buying and selling points from a given vector of integers? public static int [ ]

can you make a java method that gets the optimal buying and selling points from a given vector of integers?
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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!