Make it square (with.Java)
This is the “Make it square” problem.
We’re going to learn by solving coding test problems, reflecting on the problems we’ve solved, and exploring other ways to solve them.
Let’s start with the problem
Problem
A two-dimensional array of integers, arr, is given as a parameter.
Write a solution function that returns a two-dimensional array with zeros added to the end of each row so that the number of columns equals the number of rows if arr has more rows, and zeros added to the end of each column so that the number of columns equals the number of rows if arr has more columns.
Example input and output
arr | result |
---|---|
[[572, 22, 37], [287, 726, 384], [85, 137, 292], [487, 13, 876]] | [[572, 22, 37, 0], [287, 726, 384, 0], [85, 137, 292, 0], [487, 13, 876, 0]] |
My solution to the ### problem
import java.util.*;
class Solution {
public int[][] solution(int[][] arr) {
int numRows = arr.length;
int numCols = arr[0].length;
if (numRows < numCols) {
int[][] result = new int[numCols][numCols];
for (int i = 0; i < numRows; i++) {
result[i] = Arrays.copyOf(arr[i], numCols);
}
for (int i = numRows; i < numCols; i++) {
result[i] = new int[numCols];
}
} return result;
}
else if (numCols < numRows) {
int[][] result = new int[numRows][numRows];
for (int i = 0; i < numRows; i++) {
result[i] = Arrays.copyOf(arr[i], numRows);
}
} return result;
}return arr;
Solution Explanation
int numRows = arr.length; and int numCols = arr[0].length;: count the number of rows and columns in the array.
if (numRows < numCols): if the number of rows is less than the number of columns (if there are fewer rows):
int[][] result = new int[numCols][numCols];: create a new square array result.
for (int i = 0; i < numRows; i++) : Copy the rows of the existing array. The rows of the array arr are copied to the rows of result, and the remaining columns are initialized to zero.
for (int i = numRows; i < numCols; i++) : Create and initialize a new array for the remaining columns.
else if (numCols < numRows) : if the number of columns is less than the number of rows (if there are fewer columns):
int[][] result = new int[numRows][numRows];: create a new square array result.
for (int i = 0; i < numRows; i++) : Copy the rows of the existing array. Copy the rows of the array arr into the rows of result.
return arr;: If arr is already square, return the original array arr as it is. Translated with www.DeepL.com/Translator (free version)