Sunday, February 26, 2023
HomeSoftware EngineeringFind out how to Sum Consecutives in Java

Find out how to Sum Consecutives in Java

The problem#

You’re given a listing/array which incorporates solely integers (constructive and unfavorable). Your job is to sum solely the numbers which might be the identical and consecutive. The outcome must be one checklist.

Additional credit score in case you clear up it in a single line. You may assume there’s by no means an empty checklist/array and there’ll all the time be an integer.

Similar that means: 1 == 1

1 != -1

Examples:

``````[1,4,4,4,0,4,3,3,1] # ought to return [1,12,0,4,6,1]

"""In order you possibly can see sum of consecutives 1 is 1
sum of three consecutives 4 is 12
sum of 0... and sum of two
consecutives 3 is 6 ..."""

[1,1,7,7,3] # ought to return [2,14,3]
[-5,-5,7,7,12,0] # ought to return [-10,14,12,0]
``````

The answer in Java code#

Choice 1:

``````import java.util.*;
public class Consecutives {
public static Record<Integer> sumConsecutives(Record<Integer> s) {
int earlier = Integer.MAX_VALUE;
LinkedList<Integer> l = new LinkedList<>();
for (Integer v: s){
l.add(v == earlier? l.pollLast() + v : v);
earlier = v;
}
return l;
}
}
``````

Choice 2:

``````import java.util.ArrayList;
import java.util.Record;
public class Consecutives {
public static Record<Integer> sumConsecutives(Record<Integer> s) {
Record<Integer> accumulator = new ArrayList<>();
for (int i = 0, sum = 0; i < s.measurement(); i++) {
sum += s.get(i);
if (i == s.measurement() - 1 || s.get(i) != s.get(i + 1)) {
accumulator.add(sum);
sum = 0;
}
}
return accumulator;
}
}
``````

Choice 3:

``````import java.util.*;
public class Consecutives {
public static Record<Integer> sumConsecutives(Record<Integer> checklist) {
Record<Integer> outcome = new ArrayList<>();
int sum = checklist.get(0);
int i = 0;
whereas (i < checklist.measurement()){
if (i == checklist.measurement() - 1) {
outcome.add(sum);
} else if (checklist.get(i).equals(checklist.get(i + 1))) {
sum += checklist.get(i);
} else {
outcome.add(sum);
sum = checklist.get(i + 1);
}
i++;
}
return outcome;
}
}
``````

Check circumstances to validate our answer#

``````import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Record;
import org.junit.Check;

public class ConsecutivesTest {

@Check
public void take a look at() {
System.out.println("Fundamental Assessments");
Record<Integer> i = Arrays.asList(1,4,4,4,0,4,3,3,1);
Record<Integer> o = Arrays.asList(1,12,0,4,6,1);
System.out.println("Enter: {1,4,4,4,0,4,3,3,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(-5,-5,7,7,12,0);
o = Arrays.asList(-10,14,12,0);
System.out.println("Enter: {-5,-5,7,7,12,0}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,1,7,7,3);
o = Arrays.asList(2,14,3);
System.out.println("Enter: {1,1,7,7,3}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(3,3,3,3,1);
o = Arrays.asList(12, 1);
System.out.println("Enter: {3,3,3,3,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(2,2,-4,4,5,5,6,6,6,6,6,1);
o = Arrays.asList(4, -4, 4, 10, 30, 1);
System.out.println("Enter: {2,2,-4,4,5,5,6,6,6,6,6,1}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,1,1,1,1,3);
o = Arrays.asList(5, 3);
System.out.println("Enter: {1,1,1,1,1,3}");
assertEquals(o, Consecutives.sumConsecutives(i));
i = Arrays.asList(1,-1,-2,2,3,-3,4,-4);
o = Arrays.asList(1, -1, -2, 2, 3, -3, 4, -4);
System.out.println("Enter: {1,-1,-2,2,3,-3,4,-4}");
assertEquals(o, Consecutives.sumConsecutives(i));

}

}
``````

RELATED ARTICLES