Home > Backend Development > PHP Tutorial > Convert Array Into Array

Convert Array Into Array

王林
Release: 2024-09-03 16:08:32
Original
801 people have browsed it

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:

Convert Array Into Array

  • 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:

Convert Array Into Array

  • 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:

  1. When is it possible to convert original into a 2D array and when is it impossible?
  2. It is possible if and only if m * n == original.length
  3. 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:

  1. 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.

  2. 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));
Copy after login

The output will be:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
        )
)
Copy after login

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:

  • LinkedIn
  • GitHub

The above is the detailed content of Convert Array Into Array. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template