Wednesday, December 6, 2023
HomeSoftware EngineeringSum Consecutives in Java

Sum Consecutives in Java


The problem

You’re given an inventory/array which incorporates solely integers (constructive and unfavourable). Your job is to sum solely the numbers which can be the identical and consecutive. The outcome needs to be one checklist.

Further credit score for those who resolve it in a single line. You’ll be able to 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

Possibility 1:

import java.util.*;
public class Consecutives {
    public static Checklist<Integer> sumConsecutives(Checklist<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; 
    }
}

Possibility 2:

import java.util.ArrayList;
import java.util.Checklist;
public class Consecutives {
    public static Checklist<Integer> sumConsecutives(Checklist<Integer> s) {
        Checklist<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;
    }
}

Possibility 3:

import java.util.*;
public class Consecutives {
    public static Checklist<Integer> sumConsecutives(Checklist<Integer> checklist) {
        Checklist<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 instances to validate our answer

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

public class ConsecutivesTest {

    @Check
    public void take a look at() {
        System.out.println("Primary Exams");
        Checklist<Integer> i = Arrays.asList(1,4,4,4,0,4,3,3,1);
        Checklist<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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments