Home > Article > Backend Development > Convert Array Into Array
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:
Example 2:
Example 3:
Constraints:
Hint:
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 to m * n, where m is the number of rows and n 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 and n 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: [] ?> <h3> Explanation: </h3> <ul> <li> <p><strong>Input Validation</strong>:</p> <ul> <li>We first calculate the length of the original array.</li> <li>If the length does not equal m * n, the conversion is impossible, and we return an empty array.</li> </ul> </li> <li> <p><strong>2D Array Construction</strong>:</p> <ul> <li>We initialize a 2D array named $result.</li> <li>We use a nested loop where the outer loop runs m times (for each row) and the inner loop runs n times (for each column in a row).</li> <li>We maintain an index $index that tracks our position in the original array, incrementing it as we place elements into the 2D array.</li> </ul> </li> </ul> <h3> Example Output: </h3> <p>For the provided example:<br> </p> <pre class="brush:php;toolbar:false">$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:
The above is the detailed content of Convert Array Into Array. For more information, please follow other related articles on the PHP Chinese website!