Here is the Java solution. I am just formatting @user17315860 answer.
His answer is correct but not at all ridable.
import java.util.ArrayList;
class Solution {
static void solution(int[] A, int K) {
int maxLength = 0;
ArrayList<Integer> arr = new ArrayList<>();
for (int x : A) {
arr.add(x);
}
for (int i = 0; i < A.length; i++) {
if (maxLength < Integer.toString(A[i]).length()) {
maxLength = Integer.toString(A[i]).length();
}
}
int completeRow = A.length / K;
int iRow = A.length % K;
if (completeRow > 0) {
printDivider(K, maxLength, arr);
} else {
printDivider(iRow, maxLength, arr);
}
System.out.println();
for (int i = 0; i < completeRow; i++) {
printNumberRow(K, maxLength, arr);
System.out.println();
printDivider(K, maxLength, arr);
System.out.println();
}
if (iRow > 0) {
printNumberRow(iRow, maxLength, arr);
System.out.println();
printDivider(iRow, maxLength, arr);
System.out.println();
}
}
static void printDivider(int cells, int maxLength, ArrayList<Integer> A) {
String row = "+";
for (int i = 0; i < cells; i++) {
for (int j = 0; j < maxLength; j++) {
row += "-";
}
row += "+";
}
System.out.print(row);
}
static public void printNumberRow(int cells, int maxLength, ArrayList<Integer> A) {
String row = "|";
for (int i = 0; i < cells; i++) {
if (A.size() != 0) {
int num = A.get(0);
A.remove(0);
// String currentNumber = Integer.toString(num);
String aa = "";
int intlength = Integer.toString(num).length();
for (int j = 0; j < maxLength - intlength; j++) {
aa = aa + " ";
}
aa = aa + Integer.toString(num);
row += aa;
}
// else{
// row+= ''.padStart(maxLength, ' ');
// }
row += '|';
}
System.out.print(row);
}
public static void main(String[] args) {
int[] A = new int[] { 4, 35, 80, 123, 12345, 44, 8, 5, 24, 3,22,35 };
solution(A, 4);
}
}
K- is it not possible to use the length of the array?