You’re given an array containing both positive and negative integers and required to find the sub-a
2012-08-22 19:01
447 查看
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);
}
}
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);
}
}
相关文章推荐
- Given an array of positive and negative integers find the first subarray with zero sum
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- Rearrange an array of positive and negative integers
- I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o
- Given an unsorted integer array, find the first missing positive integer.
- Given an array of integers, sort the array according to frequency of elements
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Given an array of size n, find all the possible sub set of the array of size k
- Given a int array, please find the sub array wich has maximum sum, return the index, length and sum
- given an array of integers Find the index of values that satisfy A+B = C + D
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- You're given a string of lower-case Latin letters. Your task is to find the length of its longest su
- Given an array say [9,20,-2,-45,23,5,1], find the minimum positive missing number from the array.
- Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created.
- Data Structure Array: Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
- Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"
- 转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- 78.You are managing an Oracle Database 11g database. The database is open, and you plan to perform R