i'm writing brute force algorithm to find the maximum contiguous sub-sequence in an array
the program must display the maximum value & the sub-sequence ,,, I did it but still does not display the sub-sequence. it displays only the max value.
for example: For the array {5,-8,-4,50,-1,10,-9}, the answer is {50,-1,10} whose sum is 59
import java.util.Scanner;
public class Algorithm {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Size: ");
int s=console.nextInt();
Algorthim obj= new Algorthim();
int[] array=new int[s];
System.out.println("enter numbers");
for(int i=0; i<array.length; i++){
array[i]=console.nextInt();
}obj.FindSeq(array,array.length);
}
public void FindSeq (int A[], int n){
int [] arr2 = new int [n*(n-1)];
int sum =0;
int max=0;
for(int i=0; i<n; i++){
for(int j=i; j<=i; j++){
sum=sum+A[j];
System.out.println("sum A["+j+"]= " +sum);
for(int c=0; c<=j; c++){
if(arr2[c]== 0){
arr2[c]=sum;
System.out.println("arr2["+c+"]="+arr2[c]);
}
if(arr2[c]> max)
max = arr2[c];
}
}
}System.out.println("max ="+max);
}
}

New Topic/Question
Reply




MultiQuote




|