How Can I print one dimension array of integer into two dimensional array
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
java arrays
add a comment |
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
java arrays
What have you tried so far?
– secret super star
Nov 21 '18 at 15:02
So what have you tried?
– lostbard
Nov 21 '18 at 15:03
I don't understand how p=3 and q=4 for the given input are supposed to give you that output. It doesn't seem to follow any logic that I can see wrt order. It contains all the numbers in a 3x4 grid, but the order seems fairly random.
– Michael
Nov 21 '18 at 15:05
Possible duplicate of How to convert a 1d array to 2d array?
– Bradley Juma
Nov 21 '18 at 15:06
@Michael, the numbers are filled in an anti-diagonal fashion always
– mettleap
Nov 21 '18 at 15:10
add a comment |
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
java arrays
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
java arrays
java arrays
edited Nov 21 '18 at 16:00
Nicholas K
6,16751031
6,16751031
asked Nov 21 '18 at 15:01
Abu MarufAbu Maruf
53
53
What have you tried so far?
– secret super star
Nov 21 '18 at 15:02
So what have you tried?
– lostbard
Nov 21 '18 at 15:03
I don't understand how p=3 and q=4 for the given input are supposed to give you that output. It doesn't seem to follow any logic that I can see wrt order. It contains all the numbers in a 3x4 grid, but the order seems fairly random.
– Michael
Nov 21 '18 at 15:05
Possible duplicate of How to convert a 1d array to 2d array?
– Bradley Juma
Nov 21 '18 at 15:06
@Michael, the numbers are filled in an anti-diagonal fashion always
– mettleap
Nov 21 '18 at 15:10
add a comment |
What have you tried so far?
– secret super star
Nov 21 '18 at 15:02
So what have you tried?
– lostbard
Nov 21 '18 at 15:03
I don't understand how p=3 and q=4 for the given input are supposed to give you that output. It doesn't seem to follow any logic that I can see wrt order. It contains all the numbers in a 3x4 grid, but the order seems fairly random.
– Michael
Nov 21 '18 at 15:05
Possible duplicate of How to convert a 1d array to 2d array?
– Bradley Juma
Nov 21 '18 at 15:06
@Michael, the numbers are filled in an anti-diagonal fashion always
– mettleap
Nov 21 '18 at 15:10
What have you tried so far?
– secret super star
Nov 21 '18 at 15:02
What have you tried so far?
– secret super star
Nov 21 '18 at 15:02
So what have you tried?
– lostbard
Nov 21 '18 at 15:03
So what have you tried?
– lostbard
Nov 21 '18 at 15:03
I don't understand how p=3 and q=4 for the given input are supposed to give you that output. It doesn't seem to follow any logic that I can see wrt order. It contains all the numbers in a 3x4 grid, but the order seems fairly random.
– Michael
Nov 21 '18 at 15:05
I don't understand how p=3 and q=4 for the given input are supposed to give you that output. It doesn't seem to follow any logic that I can see wrt order. It contains all the numbers in a 3x4 grid, but the order seems fairly random.
– Michael
Nov 21 '18 at 15:05
Possible duplicate of How to convert a 1d array to 2d array?
– Bradley Juma
Nov 21 '18 at 15:06
Possible duplicate of How to convert a 1d array to 2d array?
– Bradley Juma
Nov 21 '18 at 15:06
@Michael, the numbers are filled in an anti-diagonal fashion always
– mettleap
Nov 21 '18 at 15:10
@Michael, the numbers are filled in an anti-diagonal fashion always
– mettleap
Nov 21 '18 at 15:10
add a comment |
3 Answers
3
active
oldest
votes
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
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
add a comment |
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)
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
add a comment |
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.
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414851%2fhow-can-i-print-one-dimension-array-of-integer-into-two-dimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
add a comment |
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
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
add a comment |
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
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
edited Nov 21 '18 at 16:01
answered Nov 21 '18 at 15:54
SandSand
1,417319
1,417319
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
add a comment |
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
Thank you, this is what I am looking for.
– Abu Maruf
Nov 22 '18 at 12:37
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
@AbuMaruf You're welcome
– Sand
Nov 22 '18 at 12:38
add a comment |
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)
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
add a comment |
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)
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
add a comment |
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)
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)
answered Nov 21 '18 at 15:28
mettleapmettleap
1,080216
1,080216
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
add a comment |
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
Okay, thank you, I'll try your method
– Abu Maruf
Nov 21 '18 at 15:34
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
edited Nov 21 '18 at 23:22
LukeDev
205
205
answered Nov 21 '18 at 15:38
Giuseppe D.Giuseppe D.
32
32
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
add a comment |
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
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
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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414851%2fhow-can-i-print-one-dimension-array-of-integer-into-two-dimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried so far?
– secret super star
Nov 21 '18 at 15:02
So what have you tried?
– lostbard
Nov 21 '18 at 15:03
I don't understand how p=3 and q=4 for the given input are supposed to give you that output. It doesn't seem to follow any logic that I can see wrt order. It contains all the numbers in a 3x4 grid, but the order seems fairly random.
– Michael
Nov 21 '18 at 15:05
Possible duplicate of How to convert a 1d array to 2d array?
– Bradley Juma
Nov 21 '18 at 15:06
@Michael, the numbers are filled in an anti-diagonal fashion always
– mettleap
Nov 21 '18 at 15:10