import java.util.Iterator;
import java.util.TreeMap;
public class MaximumContiguousSum {
private TreeMap tm=null;
private int[] inputArr={-1,-2,-3,6,-4,7,8};
public static void main(String[] args)
{
MaximumContiguousSum maxSum=new MaximumContiguousSum();
maxSum.populateSum(new TreeMap<Integer,String>());
}
private void populateSum(TreeMap<Integer, String> treeMap) {
// TODO Auto-generated method stub
tm=treeMap;
for(int i=0;i<inputArr.length;i++)
{
int sum=0;
StringBuffer combinations=new StringBuffer();
combinations.append(inputArr[i]);
for(int j=i+1;j<inputArr.length;j++)
{
combinations.append(inputArr[j]);
sum=sum+inputArr[j];
tm.put(sum,combinations);
}
}
Iterator sumIter=tm.descendingKeySet().iterator();
while(sumIter.hasNext())
{
int key=(Integer)sumIter.next();
System.out.println("The Maximum key is =="+key+"===Contiguous Sequence is==="+tm.get(key));
break;
}
}
}
import java.util.TreeMap;
public class MaximumContiguousSum {
private TreeMap tm=null;
private int[] inputArr={-1,-2,-3,6,-4,7,8};
public static void main(String[] args)
{
MaximumContiguousSum maxSum=new MaximumContiguousSum();
maxSum.populateSum(new TreeMap<Integer,String>());
}
private void populateSum(TreeMap<Integer, String> treeMap) {
// TODO Auto-generated method stub
tm=treeMap;
for(int i=0;i<inputArr.length;i++)
{
int sum=0;
StringBuffer combinations=new StringBuffer();
combinations.append(inputArr[i]);
for(int j=i+1;j<inputArr.length;j++)
{
combinations.append(inputArr[j]);
sum=sum+inputArr[j];
tm.put(sum,combinations);
}
}
Iterator sumIter=tm.descendingKeySet().iterator();
while(sumIter.hasNext())
{
int key=(Integer)sumIter.next();
System.out.println("The Maximum key is =="+key+"===Contiguous Sequence is==="+tm.get(key));
break;
}
}
}
No comments:
Post a Comment