Problem
Ask
Submissions

Problem: Convert 1D Array Into 2D Array

Easy
15 min
Explore how to convert a 1D integer array into a 2D array with given row and column counts. Learn to preserve element order while reshaping arrays and handle cases where reshaping is not possible. This lesson guides you through implementing the solution efficiently in Python.

Statement

Given a 0-indexed 1-dimensional (1D) integer array original and two integers, m and n, your task is to reshape the array into a 2-dimensional (2D) array with m rows and n columns while preserving the order of elements in original.

To construct the 2D array, the first n elements in the original should populate the first row. The next n elements should populate the second row, and so on, until all rows are filled. Your goal is to return the resulting m x n 2D array.

Note: If it is impossible to create an m x n array (e.g., if the total number of elements in the original is not equal to m * n), return an empty array.

Constraints:

  • 1<=1 <= original.length <=103<= 10^3

  • 1<=1 <= original[i] <=103<= 10^3

  • 1<=1 <= m, n <=33<= 33

Problem
Ask
Submissions

Problem: Convert 1D Array Into 2D Array

Easy
15 min
Explore how to convert a 1D integer array into a 2D array with given row and column counts. Learn to preserve element order while reshaping arrays and handle cases where reshaping is not possible. This lesson guides you through implementing the solution efficiently in Python.

Statement

Given a 0-indexed 1-dimensional (1D) integer array original and two integers, m and n, your task is to reshape the array into a 2-dimensional (2D) array with m rows and n columns while preserving the order of elements in original.

To construct the 2D array, the first n elements in the original should populate the first row. The next n elements should populate the second row, and so on, until all rows are filled. Your goal is to return the resulting m x n 2D array.

Note: If it is impossible to create an m x n array (e.g., if the total number of elements in the original is not equal to m * n), return an empty array.

Constraints:

  • 1<=1 <= original.length <=103<= 10^3

  • 1<=1 <= original[i] <=103<= 10^3

  • 1<=1 <= m, n <=33<= 33