I am trying to make a method that given an array and k (number of subarrays we want to divide it into), it will divide the said array into that number of subarrays. I made this so far:
public static int[][] slice(int[] arr, int k) {
int i, j;
int arrLength = arr.length;
double divided = arrLength / k;
int evenOdd = arrLength % k;
int chunks = (int) Math.ceil((double) arrLength / k);
int[][] output = IntStream.iterate(0, l -> l + chunks)
.limit((int) Math.ceil((double) arr.length / chunks))
.mapToObj(m -> Arrays.copyOfRange(arr, m, Math.min(arr.length, m + chunks)))
.toArray(int[][]::new);
return output;
}
However, this only works when the variable k is smaller or equal to half the size of the array we give. For example, if I give the arr {1,2,3,4,5,6,7} and choose k=5, it still separates my array into 4 subarrays.
Does anyone know how to fix this? Thanks very much in advance.