You are given a 0-indexed 1-dimensional (1D) integer array original
, and two integers, m
and n
. You are tasked with creating a 2-dimensional (2D) array with m
rows and n
columns using all the elements from original
.
The elements from indices 0
to n - 1
(inclusive) of original
should form the first row of the constructed 2D array, the elements from indices n
to 2 * n - 1
(inclusive) should form the second row of the constructed 2D array, and so on.
Return an m x n
2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
For example:
original = [1,2,3,4]
, m = 2
, and n = 2
, the output should be [[1,2],[3,4]]
.original = [1,2,3]
, m = 1
, and n = 3
, the output should be [[1,2,3]]
.original = [1,2]
, m = 1
, and n = 1
, the output should be []
.Explain your approach, its time and space complexity, and handle any edge cases. Provide code in Python to implement your solution.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to reshaping a one-dimensional list into a two-dimensional one involves checking every possible way to distribute the elements. We try different row and column combinations until a valid arrangement is found. Essentially, we exhaustively explore all configurations until we hit the right one.
Here's how the algorithm would work step-by-step:
def convert_1d_to_2d_array(original_list, num_rows, num_columns):
total_elements = len(original_list)
# Check if reshaping is possible based on total element counts.
if total_elements != num_rows * num_columns:
return []
reshaped_array = []
# Iterate through the number of rows to create.
for row_index in range(num_rows):
row = []
# Fill each row with the correct number of elements.
for column_index in range(num_columns):
element_index = row_index * num_columns + column_index
row.append(original_list[element_index])
reshaped_array.append(row)
# Return the fully constructed 2D array.
return reshaped_array
We're taking a single list and reshaping it into a grid with a specific number of rows and columns. The best way to do this is to methodically fill each spot in the grid, making sure we don't run out of elements or go past the grid's boundaries.
Here's how the algorithm would work step-by-step:
def convert_1d_to_2d(original_array, number_of_rows, number_of_columns):
array_length = len(original_array)
# Check if reshaping is possible.
if array_length != number_of_rows * number_of_columns:
return []
new_2d_array = [[0] * number_of_columns for _ in range(number_of_rows)]
original_index = 0
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
#Populate the 2D array element by element.
new_2d_array[row_index][column_index] = original_array[original_index]
original_index += 1
return new_2d_array
Case | How to Handle |
---|---|
Null or empty input array | Return an empty 2D array or throw an IllegalArgumentException, depending on requirements. |
Invalid dimensions: m * n != original array length | Return an empty 2D array or throw an IllegalArgumentException to indicate no valid conversion is possible. |
m or n is zero | Return an empty 2D array immediately as no rows or columns can be created. |
Large array size leading to potential integer overflow when calculating m * n | Check for potential integer overflow before multiplication, and if detected, throw an exception. |
Array contains negative numbers | The algorithm should handle negative numbers without any special consideration as they are simply elements in the array. |
m or n is negative | Throw IllegalArgumentException as the dimensions of the array must be positive integers. |
Array with all identical values | The algorithm constructs the 2D array regardless of the value distribution within the 1D array. |
m or n equals the length of the original array | The algorithm correctly constructs a 2D array with one row or one column as needed |