I need to build a method in Java where the input is a 2D array of integers and get as a result a 2D array of integers where each element makes reference to a position of an element in a column. Let me explain that with an example. Consider as an input for the method a 2D arrays of 5x5 as follow:
int[][] array = new int[][]{
{124, 188, 24, 254, 339},
{0, 7, 77, 145, 159},
{206, 340, 280, 523, 433},
{310, 265, 151, 411, 398},
{24, 104, 0, 183, 198}};
Now I need to build a new 2D array of integer (I will call newArray
in the following) according to:
The minimum value of column 0 in the array is 0 and is associated with row 1 (then, I need to assign a 1 in
newArray[0][0]
).Next, the minimum value of column 0 in the array is 24 and is associated with row 4 (then, I need to assign a 4 in
newArray[1][0]
).Then, the minimum value of column 0 in the array is 124 and is associated with row 0 (then, I need to assign a 0 in
newArray[2][0]
).And so on for each column...
The final output of the method must be something like the following 2d array.
Any help would be highly appreciated.
If I understood correctly :
Here's the code :
There might be an easier way, but it works !