-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.java
More file actions
50 lines (47 loc) · 1.67 KB
/
Range.java
File metadata and controls
50 lines (47 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package kyu4;
/**
* 4 kyu - Range Extraction
*
* https://www.codewars.com/kata/51ba717bb08c1cd60f00002f/java
*
* DESCRIPTION:
* 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[] {-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20})
* # returns "-10--8,-6,-3-1,3-5,7-11,14,15,17-20"
*
*
*/
import java.util.ArrayList;
import java.util.List;
public class Range {
public static String rangeExtraction(int[] arr) {
List<String> list = new ArrayList<>();
int count = 0;
for (int i = 0; i < arr.length; ) {
count = 0;
String start = String.valueOf(arr[i]);
while (i != arr.length - 1 && arr[i + 1] - arr[i] == 1) {
count++;
i++;
}
if (count > 0) {
if (count > 1) {
start += "-" + (Integer.parseInt(start) + count);
} else {
i--;
}
}
list.add(start);
i++;
}
return String.join(",", list);
}
}