import java.util.Arrays;
import java.util.List;
import java.util.stream.*;
class Main {
public static void main
(String[] args
) { String [] data
= {"europe",
"france",
"germany",
"america"}; int startIndex = 1;
int count = 2;
int newSize = data.length - count + 1;
List<String> output =
IntStream.range(0, newSize)
.mapToObj(n -> Main.getOrJoin(data, startIndex, count, n))
.collect(Collectors.toList());
}
}
private static String getOrJoin
(String[] data,
int joinIndex,
int count,
int index
) { if (index < joinIndex) return data[index];
else if (index > joinIndex) return data[index + count - 1];
else {
String[] dataToJoin
= Arrays.
copyOfRange(data, joinIndex, joinIndex
+ count
); return String.
join(" ", dataToJoin
); }
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7ICAgCmltcG9ydCBqYXZhLnV0aWwuTGlzdDsgIAppbXBvcnQgamF2YS51dGlsLnN0cmVhbS4qOwoKY2xhc3MgTWFpbiB7CiAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgU3RyaW5nIFtdIGRhdGEgPSB7ImV1cm9wZSIsICJmcmFuY2UiLCAiZ2VybWFueSIsICJhbWVyaWNhIn07CiAgICBpbnQgc3RhcnRJbmRleCA9IDE7CiAgICBpbnQgY291bnQgPSAyOwogICAgaW50IG5ld1NpemUgPSBkYXRhLmxlbmd0aCAtIGNvdW50ICsgMTsKICAgIAogICAgTGlzdDxTdHJpbmc+IG91dHB1dCA9CiAgICAgICAgSW50U3RyZWFtLnJhbmdlKDAsIG5ld1NpemUpCiAgICAgICAgICAgICAgICAgLm1hcFRvT2JqKG4gLT4gTWFpbi5nZXRPckpvaW4oZGF0YSwgc3RhcnRJbmRleCwgY291bnQsIG4pKQogICAgICAgICAgICAgICAgIC5jb2xsZWN0KENvbGxlY3RvcnMudG9MaXN0KCkpOwogICAgICAgICAgICAgICAgIAogICAgZm9yIChTdHJpbmcgcyA6IG91dHB1dCkgewogICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocyk7CiAgICB9CiAgfQogIAogIHByaXZhdGUgc3RhdGljIFN0cmluZyBnZXRPckpvaW4oU3RyaW5nW10gZGF0YSwgaW50IGpvaW5JbmRleCwgaW50IGNvdW50LCBpbnQgaW5kZXgpIHsKICAgIGlmIChpbmRleCA8IGpvaW5JbmRleCkgcmV0dXJuIGRhdGFbaW5kZXhdOwogICAgZWxzZSBpZiAoaW5kZXggPiBqb2luSW5kZXgpIHJldHVybiBkYXRhW2luZGV4ICsgY291bnQgLSAxXTsKICAgIGVsc2UgewogICAgICAgIFN0cmluZ1tdIGRhdGFUb0pvaW4gPSBBcnJheXMuY29weU9mUmFuZ2UoZGF0YSwgam9pbkluZGV4LCBqb2luSW5kZXggKyBjb3VudCk7CiAgICAgICAgcmV0dXJuIFN0cmluZy5qb2luKCIgIiwgZGF0YVRvSm9pbik7CiAgICB9CiAgfQp9