If I have array like [1,2,3,4] and k = 3 then output should be [1,2,3][2,3,4] which is in this order. The idea is to get subarray with k elements and then the next to start from the next element and also to have k elements I can't think of a way to do it more generic for any value of k.
final int arr[] = new int[] { 1, 2, 3, 4 };
final int max = 2;
for (int i = 0; i < arr.length - 1; i ) {
for (int j = i 1; j < arr.length; j ) {
for (int k = 0; k < max; k = k 2) {
System.out.println(i "" j);
}
}
}
CodePudding user response:
You can extract parts of an array by calling Arrays.copyOfRange() method:
int[] arr = {1, 2, 3, 4};
int k = 3;
int numberOfResults = arr.length - k 1;
for (int i = 0; i < numberOfResults; i ) {
int[] result = Arrays.copyOfRange(arr,i,i k);
System.out.println(Arrays.toString(result));
}