Skip to content

2022. Convert 1D Array Into 2D Array #450

Answered by topugit
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

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 I…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 1, 2024
Maintainer Author

@topugit
Comment options

topugit Sep 1, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants