Brute Force Solution of Finding Maximum-Sub Array Code.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Muneeb
*/
public class NewClass {
public static void main(String args[])
{
int a[]={-1,-7,-8,-13,-1};
int i=0;
int j=0;
int sum=-1;
int currentsum=0;
int left=0;
int right=0;
for(i=0;i<a.length;i++)
{
currentsum=0;
for(j=i;j<a.length;j++)
{
currentsum=currentsum+a[j];
if(sum<currentsum)
{
sum=currentsum;
left=i;
right=j;
}
}
}
System.out.println("Left "+left+"\nRight"+right+"\nSum"+sum);
}
}
Comments
Post a Comment