Software Engineering

# Range Extraction in Java

## The challenge#

A format for expressing an ordered list of integers is to use a comma separated list of either

• individual integers
• or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, ‘-‘. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example “12,13,15-17”

Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.

Example:

``````Solution.rangeExtraction(new int[] {-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20})
// returns "-6,-3-1,3-5,7-11,14,15,17-20"
``````

## The solution in Java code#

Option 1:

``````class Solution {
public static String rangeExtraction(int[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
int j = i;
while (j < arr.length - 1 && arr[j] + 1 == arr[j + 1]) j++;
if (i + 1 < j) {
i = j;
sb.append("-");
sb.append(arr[i]);
}
sb.append(",");
}
sb.setLength(sb.length() - 1);
return sb.toString();
}
}
``````

Option 2:

``````class Solution {
static String rangeExtraction(final int[] array) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
int rangeStart = array[i];
sb.append(rangeStart);
for (int j = i + 1; j <= array.length; j++) {
if (j == array.length || array[j] != rangeStart + (j - i)) {
if (j - i >= 3) {
sb.append('-').append(array[j - 1]);
i = j - 1;
}
sb.append(',');
break;
}
}
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
}
``````

Option 3:

``````import java.util.*;
class Solution {
public static String rangeExtraction(int[] arr) {
String res = "";
boolean isStart = true;
for (int i = 0; i < arr.length - 1; i++) {
boolean isChecked = (arr[i] + 1 != arr[i + 1]);
res += isChecked ? arr[i] + "," : isStart ? arr[i] + "~" : "~";
isStart = isChecked;
}
return res.replaceAll("([~]{2,})","-").replace("~", ",") + arr[arr.length - 1];
}
}
``````

## Test cases to validate our solution#

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
@Test
public void test_BasicTests() {
assertEquals("-6,-3-1,3-5,7-11,14,15,17-20", Solution.rangeExtraction(new int[] {-6,-3,-2,-1,0,1,3,4,5,7,8,9,10,11,14,15,17,18,19,20}));

assertEquals("-3--1,2,10,15,16,18-20", Solution.rangeExtraction(new int[] {-3,-2,-1,2,10,15,16,18,19,20}));
}
}
``````