 ## The problem#

Write a perform known as `sumIntervals`/`sum_intervals()` that accepts an array of intervals, and returns the sum of all of the interval lengths. Overlapping intervals ought to solely be counted as soon as.

Intervals

Intervals are represented by a pair of integers within the type of an array. The primary worth of the interval will at all times be lower than the second worth. Interval instance: `[1, 5]` is an interval from 1 to five. The size of this interval is 4.

Overlapping Intervals

Record containing overlapping intervals:

``````[
[1,4],
[7, 10],
[3, 5]
]
``````

The sum of the lengths of those intervals is 7. Since [1, 4] and [3, 5] overlap, we will deal with the interval as [1, 5], which has a size of 4.

Examples:

``````sumIntervals( [
[1,2],
[6, 10],
[11, 15]
] ) => 9

sumIntervals( [
[1,4],
[7, 10],
[3, 5]
] ) => 7

sumIntervals( [
[1,5],
[10, 20],
[1, 6],
[16, 19],
[5, 11]
] ) => 19

sumIntervals( [
[0, 20],
[-100000000, 10],
[30, 40]
] ) => 100000030
``````

Checks with giant intervals

Your algorithm ought to have the ability to deal with giant intervals. All examined intervals are subsets of the vary `[-1000000000, 1000000000]`.

Choice 1:

``````bundle cw;

import java.util.Arrays;
import java.util.Comparator;

public class Interval {
public static int sumIntervals(int[][] intervals) {
if (intervals == null || intervals.size < 1) {
return 0;
}
Arrays.type(intervals, Comparator.comparingInt(a -> a));
int consequence = 0;
int currentIntervalEnd = intervals;

for (int[] interval : intervals) {
int intervalStart = interval;
int intervalEnd = interval;
if (intervalEnd > currentIntervalEnd) {
consequence += intervalEnd - Math.max(intervalStart, currentIntervalEnd);
currentIntervalEnd = intervalEnd;
}
}
return consequence;
}
}
``````

Choice 2:

``````bundle cw;
import java.util.*;

public class Interval {

ultimate static non-public Comparator<int[]> CMP_RNG = Comparator.<int[]>comparingInt(rng -> rng);

public static int sumIntervals(int[][] intervals) {
if (intervals==null) return 0;

int     s      = 0,
high    = Integer.MIN_VALUE;
int[][] ranges = Arrays.copyOf(intervals, intervals.size);
Arrays.type(ranges, CMP_RNG);

for (int[] rng: ranges) {
if (high<rng)   high = rng;
if (high<rng) { s  += rng-top; high = rng; }
}
return s;
}
}
``````

Choice 3:

``````bundle cw;

import java.util.Arrays;

public class Interval {

public static int sumIntervals(int[][] intervals) {
Arrays.type(intervals, (a, b) -> Integer.examine(b, a));
return Arrays.stream(intervals)
.mapToInt(ints -> {
int[] temp = new int[]{ints, ints};
Arrays.fill(ints, Integer.MAX_VALUE);
Arrays.stream(intervals).forEach(arr -> {
if ((arr >= temp && arr <= temp)
|| (arr >= temp && arr <= temp)) {
temp = Math.min(temp, arr);
temp = Math.max(temp, arr);
Arrays.fill(arr, Integer.MAX_VALUE);
}
});
return temp - temp;
}).sum();
}
}
``````

## Check circumstances to validate our resolution#

``````import org.junit.Check;

import static cw.Interval.sumIntervals;
import static org.junit.Assert.assertEquals;

public class IntervalTest {

@Check
public void shouldHandleEmptyIntervals() {
assertEquals(0, sumIntervals(new int[][]{}));
assertEquals(0, sumIntervals(new int[][]{{4, 4}, {6, 6}, {8, 8}}));
}

@Check
assertEquals(9, sumIntervals(new int[][]{{1, 2}, {6, 10}, {11, 15}}));
assertEquals(11, sumIntervals(new int[][]{{4, 8}, {9, 10}, {15, 21}}));
assertEquals(7, sumIntervals(new int[][]{{-1, 4}, {-5, -3}}));
assertEquals(78, sumIntervals(new int[][]{{-245, -218}, {-194, -179}, {-155, -119}}));
}

@Check
public void shouldHandleLargeIntervals() {
assertEquals(2_000_000_000, sumIntervals(new int[][]{{-1_000_000_000, 1_000_000_000}}));
assertEquals(100_000_030, sumIntervals(new int[][]{{0, 20}, {-100_000_000, 10}, {30, 40}}));
}

@Check
assertEquals(54, sumIntervals(new int[][]{{1, 2}, {2, 6}, {6, 55}}));
assertEquals(23, sumIntervals(new int[][]{{-2, -1}, {-1, 0}, {0, 21}}));
}

@Check
assertEquals(7, sumIntervals(new int[][]{{1, 4}, {7, 10}, {3, 5}}));
assertEquals(6, sumIntervals(new int[][]{{5, 8}, {3, 6}, {1, 2}}));
assertEquals(19, sumIntervals(new int[][]{{1, 5}, {10, 20}, {1, 6}, {16, 19}, {5, 11}}));
}

@Check
public void shouldHandleMixedIntervals() {
assertEquals(13, sumIntervals(new int[][]{{2, 5}, {-1, 2}, {-40, -35}, {6, 8}}));
assertEquals(1234, sumIntervals(new int[][]{{-7, 8}, {-2, 10}, {5, 15}, {2000, 3150}, {-5400, -5338}}));
assertEquals(158, sumIntervals(new int[][]{{-101, 24}, {-35, 27}, {27, 53}, {-105, 20}, {-36, 26},}));
}

}
``````