Reference Question: Numbers which constitute the Maximum sum
I was writing a program which would print the elements which have constituted to the maximum sum. I have been able to pass through any random scenario but when my maximum sum constitutes of two sets, my code fails.
My code:
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
Scanner reader = new Scanner(System.in);
int TestCases = reader.nextInt();
reader.nextLine();
String[] output = new String[TestCases];
String sss = "";
String ddd = "";
for (int k = 0; k < TestCases; k++) {
int noofELements = reader.nextInt();
reader.nextLine();
String[] al = reader.nextLine().split(" ");
List<Integer> numbers = Arrays.stream(al).map(Integer::valueOf).collect(Collectors.toList());
Ideone mm = new Ideone();
String maxi = mm.maximumm(numbers, ddd);
sss = sss.concat(maxi);
}
System.out.println(sss);
}
public String maximumm(List<Integer> numbers, String sss) {
int toIndex = 3, fromIndex = 0;
List<Integer> result = new ArrayList<>();
while (toIndex < numbers.size()) {
Map<Integer, Integer> map =
IntStream.range(fromIndex, toIndex).mapToObj(i -> new AbstractMap.SimpleEntry<>(i, numbers.get(i)))
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
// find max of sublist
int maxOfSub = numbers.subList(fromIndex, toIndex).stream().max(Integer::compareTo).get();
//update indexes
fromIndex = map.get(maxOfSub) + 2;
toIndex += fromIndex;
result.add(maxOfSub);
}
int lastMax = numbers.subList(fromIndex, numbers.size()).stream().max(Integer::compareTo).get();
if (lastMax > 0) {
result.add(lastMax);
}
result = result.stream().sorted(Integer::compareTo).collect(Collectors.toList());
//System.out.println(result);
sss = sss.concat(result.toString().replace(", ", "").replace("]", "").replace("[", ""));
return sss;
// return result.stream().reduce(0,Integer::sum);
}
}
For example when i am giving a input of 4 5 4 3 , then maximum sum of non adjacent elements is 8 , which would be made from 4 4 or 5 3.
My full code is working fine, just that i am not able to get both results in my final result.
My Error Log:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 0 at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133) at java.util.HashMap.merge(HashMap.java:1254) at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320) at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) at java.util.stream.IntPipeline$4$1.accept(IntPipeline.java:250) at java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:110) at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) at Ideone.maximumm(Ideone.java:47) at Ideone.main(Ideone.java:27)
Error is pointed to this line: result.add(maxOfSub);
Any help would be nice :)
submap(k, v) -> submap(v, k)and cuzvshould be unique, in this case there are two numbers4. If he wants to get only the sum of two non adjacent elements the implementations will change. - Teocci.mapToObj(i -> new AbstractMap.SimpleEntry<>(i, numbers.get(i)))step in your operation. You can simply useIntStream.range(fromIndex, toIndex).boxed().collect(Collectors.toMap(i -> i, i -> numbers.get(i)));instead. It would also work asIntStream.range(fromIndex, toIndex).boxed().collect(Collectors.toMap(Function.identity(),numbers::get));. Of course, it doesn’t help you with the exception. There are duplicates, hence, you have to decide what should happen with the duplicates. - Holger