Skip to main content
Back to problems
#2022
Easy Algorithms

Convert 1d array into 2d array

Array Matrix Simulation
72.1% acceptance
Feb 25, 2026
1294
103
You are given a 0-indexed 1-dimensional integer array original, and two integers m and n. Create a 2D array with m rows and n columns using all elements from original. Return the m x n 2D array, or empty array if impossible.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {
    let (m, n) = (m as usize, n as usize);
    if original.len() != m * n { return vec![]; }
    original.chunks(n).map(|c| c.to_vec()).collect()
  }
}