Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

The problem is "Using divide and conquer algorithm, find a maximum subsequence sum." 
My problem is from the codes below that how it works.

"int maxLeftSum=MaxSub(Alist,Start,Center); "
I'm guessing, 
Center =3 , after MaxSub is called Center=1, after MaxSub is called Center =0, then return with 4 ?


============================================================

public class SubSequence{
public static void main(String[] args){

int Alist[]={4,-3,5,-2,-1,2,6,-2};
int Start=0;
int End=Alist.length-1; 

int num=MaxSub(Alist,Start ,End ); 
System.out.println("maxSum is: "+num);
}
public static int MaxSub(int Alist[],int Start,int End){

int leftBordersum=0, rightBordersum=0;
int maxLeftBorderSum=0, maxRightBorderSum=0; 
int maxMiddleSum;

if(Start==End)
if(Alist[Start]>0)
return Alist[Start];
else 
return 0; 

int Center=((Start+End)/2); 

int maxLeftSum=MaxSub(Alist,Start,Center); 
int maxRightSum=MaxSub(Alist,Center+1,End);

for(int i=Center; i>=Start; i--){ //Counting down 
leftBordersum+=Alist[i];
if(leftBordersum>maxLeftBorderSum)
maxLeftBorderSum=leftBordersum;
}
for(int i=Center +1; i<=End; i++){ // Counting up 
rightBordersum+=Alist[i];
if(rightBordersum>maxRightBorderSum)
maxRightBorderSum=rightBordersum;
}
maxMiddleSum=maxLeftBorderSum+maxRightBorderSum; 
int maxSum=0;

if(maxLeftSum>maxRightSum && maxLeftSum >maxMiddleSum){ //return maxLeftsum 
maxSum=maxLeftSum;
}else if(maxRightSum>maxLeftSum && maxRightSum>maxMiddleSum){
maxSum=maxRightSum;
}else if(maxMiddleSum>maxLeftSum && maxMiddleSum>maxRightSum){
maxSum=maxMiddleSum;

return maxSum;


Java, Programming

  • Category:- Java
  • Reference No.:- M999220

Have any Question?


Related Questions in Java

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As