-6

Suppose, I take a one-dimensional array of integer

A = {1,2,3,4,5,6,7,8, 9, 10, 11, 12}

Now, I want to to rearrange the integers in A in a two dimensional array with p rows and q columns in diagonal fashion. Where, p=3 and q=4.

Output will be like this:

1 2 4 7
3 5 8 10
6 9 11 12
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Abu Maruf
  • 5
  • 3

3 Answers3

0

You can try the following approach:

1) Initialize a 2d array of the specified dimension, i.e p by q
2) For each cell in the first row, fill its anti-diagonal ( like from north-east to south-west direction) with the next available elements in your 1d array.
3) After the first row is processed, similarly do step 2 for each remaining cell in the last column of the 2d array.

Finally, your 2d array will hold the answer you want (i.e numbers filled in an anti-diagonal fashion)

mettleap
  • 1,390
  • 8
  • 17
0

To have this result you could do this:

int z = 0;
for(int i = 0; i < 3; i++){
    for (int j = 0; j < 4; j++){
       System.out.print(A[z]);
       z++;
    }
   System.out.println();
}

This uses nested for loops to loop through the i and j, equivalent to the p and q rows and columns. The inner loop includes a z counter, which is the index in the original array. Be sure to include a definition of A[] in your code.

LukeDev
  • 20
  • 1
  • 5
  • Welcome to SO, Giuseppe D.! Code-only answers are discouraged here, as they provide no insight into how the problem was solved. Please update your solution to include an explanation of how your code solves the problem at hand :) – Joel Nov 21 '18 at 15:44
0

You can do it like this,

public class Main {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        int[][] diagonalArray = createDiagonalArray(array, 3, 4);
        print2DArray(diagonalArray);
    }

    private static int[][] createDiagonalArray(int[] array, int p, int q) {

        int[][] input = new int[p][q];
        for (int j = 0; j < p; j++) {
            for (int i = 0; i < q; i++) {
                input[j][i] = array[j * q + i];
            }
        }

        final int numRows = input.length;
        final int numColumns = input[0].length;
        int[][] result = new int[numRows][numColumns];

        int rowIndex = 0;
        int columnIndex = 0;
        int currentRow = 0;
        int currentColumn = 0;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numColumns; j++) {
                result[currentRow][currentColumn] = input[i][j];
                if (currentRow == numRows - 1) {
                    if (numRows < numColumns && columnIndex < numColumns - 1) {
                        currentRow = 0;
                        currentColumn = ++columnIndex;
                    } else {
                        currentRow = ++rowIndex;
                        currentColumn = numColumns - 1;
                    }
                } else if (currentColumn == 0) {
                    if (columnIndex < numColumns - 1) {
                        currentRow = rowIndex;
                        currentColumn = ++columnIndex;
                    } else {
                        currentColumn = columnIndex;
                        currentRow = ++rowIndex;
                    }
                } else {
                    currentRow++;
                    currentColumn--;
                }

            }
        }
        return result;
    }

    private static void print2DArray(int[][] diagonalArray) {
        for (int j = 0; j < 3; j++) {
            for (int i = 0; i < 4; i++) {
                System.out.print(diagonalArray[j][i] + " ");
            }
            System.out.println();
        }
    }
}

The two dimensional part was taken from here

Sandeepa
  • 3,457
  • 5
  • 25
  • 41