I'm trying to understand this 2D Arrays and just read this code and problem on CodeCademy. It seems like the logic is wrong. Why is it limiting the outer pass into arr[0].length only? What if it's not an equal n x n dimensions?
Are 2D arrays supposed to have an equal n x n dimension?
class Name {
public static int largestColumn(int[][] nums){
int largestColumn = 0;
int largestSum = 0;
for (int i = 0; i < arr2D[0].length; i ) {
int columnVal = 0;
for (int j = 0; j < arr2D.length; j ) {
columnVal = arr2D[j][i];
}
if (columnVal > largestSum) {
largestSum = columnVal;
largestColumn = i;
}
}
return largestColumn;
}
public static void main(String[] args){
int[][] values = {
{ 17, 13, 19, 22 },
{ 12, 18, 25, 20 },
{ 15, 18, 21, 24 },
{ 19, 23, 23, 22 },
{ 18, 20, 21, 26}
};
System.out.println(largestColumn(values)); // 3
}
}
but what if you have these in the as values?
int[][] values = {
{ 17, 13, 19, 22 },
{ 12, 18, 25, 20 },
{ 15, 18, 21, 24 },
{ 19, 23, 23, 22 },
{ 18, 20, 21, 26, 1000, 500}
};
Obviously, column 4 will be the largest. but with the solution above, with arr[0].length as the first pass max, it won't pass through col 4 and col 5 as the max first pass would be < 3.
I checked the lengths of the 2nd 2D array values, 2D arrays are not self filling if you fill another array[i] with more elements (maybe just in Java since it's fixed?)
Any help would be appreciated, thanks!
CodePudding user response:
The following function should work (explanation below):
public static int largestColumn(int[][] values){
int maxLength = 0;
for(int i = 0; i < values.length; i ){
if(values[i].length > maxLength){
maxLength = values[i].length;
}
}
int maxSum = 0;
int maxInd = 0;
for(int i = 0; i < maxLength; i ){
int currSum = 0;
for(int j = 0; j < values.length; j ){
if(values[j].length > i){
currSum = values[j][i];
}
}
if(maxSum < currSum){
maxSum = currSum;
maxInd = i;
}
}
return maxInd;
}
Input #1:
public static void main(String[] args) {
int[][] values = {
{ 17, 13, 19, 22 },
{ 12, 18, 25, 20 },
{ 15, 18, 21, 24 },
{ 19, 23, 23, 22 },
{ 18, 20, 21, 26}
};
System.out.println(largestColumn(values));
}
Output:
3
Input #2:
public static void main(String[] args) {
int[][] values = {
{ 17, 13, 19, 22 },
{ 12, 18, 25, 20 },
{ 15, 18, 21, 24 },
{ 19, 23, 23, 22 },
{ 18, 20, 21, 26, 1000, 500}
};
System.out.println(largestColumn(values));
}
Output:
4
First, we need to determine what the largest length of any row is. By simply traversing through every low and using a maximum algorithm, we can store the length of the largest row of values
in maxLength
.
Next, we iterate through all of the columns by having the variable, i
, iterate from 0
to maxLength
. In every column i
, we add up the values in the i
'th column, and store the value in currSum
. When we reach a new maximum sum, we store the value in maxSum
and the index in maxInd
.
However, as you noticed, if values
is not an nxn grid, we may get an out of bounds error. This is why we add an extra conditional to check if the j
th row we are iterating through when calculating the sum has an i
th column.
I hope this helped! Please let me know if you need any further help or clarification :)
CodePudding user response:
A Two Dimensional Array in Java is an Array of Arrays and those inner arrays can be any length.
To read a 2D Array, read the Row then the columns of that row. A row is in the horizontal plane of the Matrix and the columns are in the vertical plain on the matrix, just like looking at a data table
In the following non-square matrix:
int[][] values = {//C0 C1 C2 C3 C4 C5
{ 17, 13, 19, 22 }, // Row 0
{ 12, 18, 25, 20 }, // Row 1
{ 15, 18, 21, 24 }, // Row 2
{ 19, 23, 23, 22 }, // Row 3
{ 18, 20, 21, 26, 1000, 500}, // Row 4
{ 27, 28, 29} // Row 5
};
the first row (row 0) consists of: 17, 13, 19, 22
, the second row (row 1) consists of: 12, 18, 25, 20
, and so on. In Row 0 the 17
is column 0, 13 is column 1, 19 is column 2, and so on.
To find Array with the greatest number of columns within the above 2D Array, you can do this:
int[][] values = {
{ 17, 13, 19, 22 },
{ 12, 18, 25, 20 },
{ 15, 18, 21, 24 },
{ 19, 23, 23, 22 },
{ 18, 20, 21, 26, 1000, 500},
{ 27, 28, 29}
};
System.out.println("The 2D Array:");
int maxLen = values[0].length;
int indexOfMax = -1;
for (int i = 0; i < values.length; i ) {
System.out.println(Arrays.toString(values[i]));
if (values[i].length > maxLen) {
maxLen = values[i].length;
indexOfMax = i;
}
}
System.out.println();
System.out.println("Literal Row #" (indexOfMax 1) " (index: "
indexOfMax ") has the\ngreatest number columns: "
maxLen);
System.out.println(Arrays.toString(values[indexOfMax]));
The Console Window should display:
The 2D Array:
[17, 13, 19, 22]
[12, 18, 25, 20]
[15, 18, 21, 24]
[19, 23, 23, 22]
[18, 20, 21, 26, 1000, 500]
[27, 28, 29]
Literal Row #5 (index: 4) has the
greatest number columns: 6
[18, 20, 21, 26, 1000, 500]