2022. Convert 1D Array Into 2D Array
Difficulty: Easy
Topics: Array
, Matrix
, Simulation
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.
Example 1:
- Input: original = [1,2,3,4], m = 2, n = 2
- Output: [[1,2],[3,4]]
-
Explanation:
- The constructed 2D array should contain 2 rows and 2 columns.
- The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
- The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.
Example 2:
- Input: original = [1,2,3], m = 1, n = 3
- Output: [[1,2,3]]
-
Explanation:
- The constructed 2D array should contain 1 row and 3 columns.
- Put all three elements in original into the first row of the constructed 2D array.
Example 3:
- Input: original = [1,2], m = 1, n = 1
- Output: []
-
Explanation:
- There are 2 elements in original.
- It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.
Constraints:
1 <= original.length <= 5 * 104
1 <= original[i] <= 105
1 <= m, n <= 4 * 104
Hint:
- When is it possible to convert original into a 2D array and when is it impossible?
- It is possible if and only if m * n == original.length
- If it is possible to convert original to a 2D array, keep an index i such that original[i] is the next element to add to the 2D array.
Solution:
We need to follow these steps:
Check if Conversion is Possible: The conversion from a 1D array to a 2D array is only possible if the total number of elements in the 1D array (
original.length
) is exactly equal tom * n
, wherem
is the number of rows andn
is the number of columns. If this condition is not met, return an empty array.Create the 2D Array: If the conversion is possible, initialize a 2D array with
m
rows andn
columns, and populate it by iterating over the 1D array and filling in the 2D array row by row.
Let's implement this solution in PHP: 2022. Convert 1D Array Into 2D Array
<?php
/**
* @param Integer[] $original
* @param Integer $m
* @param Integer $n
* @return Integer[][]
*/
function construct2DArray($original, $m, $n) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
//Example 1
$original = array(1, 2, 3, 4);
$m = 2;
$n = 2;
print_r(construct2DArray($original, $m, $n)); //Output: [[1,2],[3,4]]
//Example 2
$original = array(1, 2, 3);
$m = 1;
$n = 3;
print_r(construct2DArray($original, $m, $n)); //Output: [[1,2,3]]
//Example 3
$original = array(1, 2);
$m = 1;
$n = 1;
print_r(construct2DArray($original, $m, $n)); //Output: []
?>
Explanation:
-
Input Validation:
- We first calculate the length of the original array.
- If the length does not equal
m * n
, the conversion is impossible, and we return an empty array.
-
2D Array Construction:
- We initialize a 2D array named
$result
. - We use a nested loop where the outer loop runs
m
times (for each row) and the inner loop runsn
times (for each column in a row). - We maintain an index
$index
that tracks our position in the original array, incrementing it as we place elements into the 2D array.
- We initialize a 2D array named
Example Output:
For the provided example:
$original = array(1, 2, 3, 4);
$m = 2;
$n = 2;
print_r(construct2DArray($original, $m, $n));
The output will be:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
This approach ensures that the 1D array is correctly converted into the desired 2D array, and it efficiently handles edge cases by checking whether the conversion is possible.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me: