0
点赞
收藏
分享

微信扫一扫

You’re given an array containing both positive and negative integers and required to find the sub-a


You’re given an array containing both positive and negative integers and required to find the sub-array with the largest sum

 

import java.util.ArrayList;
import java.util.List;
public class Duplicate {

public static int sum(List<Integer> wholeList,int start,int end){

int sum = 0;
for(int i=start;i<=end;i++)
{
sum=sum+wholeList.get(i);
}
return sum;
};


public static void main(String args[]){
int N=10;
List<Integer> wholeList=new ArrayList<Integer>();
for(int i=0;i<N;i++)
{
wholeList.add((int)(Math.random()*N)-N/2);
System.out.print(wholeList.get(i)+" ");
}



int maxSum=sum(wholeList,0,0);
int I = 0,J = 0;

for(int i=0;i<N;i++){
for(int j=i;j<N;j++){

if(sum(wholeList,i,j)>maxSum){maxSum=sum(wholeList,i,j);
I=i;
J=j;
}
}}

System.out.println();
System.out.print(maxSum +" "+I+" "+J);
}
}

 

举报

相关推荐

0 条评论