Working with Eclipse IDE but not on InterviewBit console ; Throwing error for A=58;


#1

public class Solution {
static int count =1;
public ArrayList<ArrayList> generateMatrix(int A) {
// TODO Auto-generated method stub
int i = 0;
int j = 0;
int itr = 0;
Integer arr[][] = new Integer[A][A];
if(A/2 != 0) {
arr[A/2][A/2] = A*A;
}
while (itr < A / 2) {
j = fillRight(itr, itr, A, arr);
i = fillDown(i + 1, j - 1, itr, A, arr);
j = fillLeft(i - 1, j - 2, itr, A, arr);
i = fillUp(i - 2, j+1, itr, A, arr);
itr++;
i++;
j++;
}
ArrayList<ArrayList> lists = new ArrayList<>();
for (Integer [] ints : arr) {
lists.add(new ArrayList(Arrays.asList(ints)));
}
return lists;
}

private static int fillUp(int i, int j, int itr, int a, Integer[][] arr) {
    // TODO Auto-generated method stub
    ArrayList<Integer> arr1 = new ArrayList();
    while (i >  itr) {
        arr[i][j] = count;
        count++;
        i--;

    }
    return i;
}

private static int fillLeft(int i, int j, int itr, int a, Integer[][] arr) {
    // TODO Auto-generated method stub
    ArrayList<Integer> arr1 = new ArrayList();
    while (j >= itr) {
        arr[i][j] = count;
        count++;
        j--;
    }
    return j;
}

private static int fillDown(int i, int j, int itr, int a, Integer[][] arr) {
    // TODO Auto-generated method stub
    ArrayList<Integer> arr1 = new ArrayList();
    while (i < a - itr) {
        arr[i][j] = count;
        count++;
        i++;
    }
    return i;
}

private static int fillRight(int j, int itr, int a, Integer[][] arr) {
    // TODO Auto-generated method stub
    ArrayList<Integer> arr1 = new ArrayList();
    while (j < a - itr) {
        arr[itr][j] = count;
        count++;
        j++;
    }
    return j;
}

}


#2

Not able to get much from the code. Could you please explain bit ?


#3

i am using itr variable to keep count of rounds , like if outer most round is printing going to inner round and so on.


#4

Even i am gettting the same problem on A=58. Pasted my code and wrote the main driver function in ideone.
It runs smoothly.