Tuesday, April 23, 2024
HomeSoftware EngineeringHow one can Sum Consecutives in Java

How one can Sum Consecutives in Java


The problem

You might be given an inventory/array which accommodates solely integers (constructive and unfavorable). Your job is to sum solely the numbers which might be the identical and consecutive. The outcome needs to be one listing.

Further credit score if you happen to remedy it in a single line. You’ll be able to assume there may be by no means an empty listing/array and there’ll at all times be an integer.

Identical 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'll be able to 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> listing) {
        Checklist<Integer> outcome = new ArrayList<>();
        int sum = listing.get(0);
        int i = 0;
        whereas (i < listing.measurement()){
            if (i == listing.measurement() - 1) {
                outcome.add(sum);
            } else if (listing.get(i).equals(listing.get(i + 1))) {
                sum += listing.get(i);
            } else {
                outcome.add(sum);
                sum = listing.get(i + 1);
            }
            i++;
        }
        return outcome;
    }
}

Take a look at instances to validate our resolution

import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Checklist;
import org.junit.Take a look at;

public class ConsecutivesTest {

    @Take a look at
    public void check() {
        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