Convert 1D Array Into 2D Array

You are given a 0-indexed integer array `original` and two integers `m` and `n`. Build an `m x n` 2D array from all the values of `original`, in order: indexes `0` to `n - 1` form row 0, indexes `n` to `2n - 1` form row 1, and so on. If `original` cannot fill an `m x n` array exactly, return an empty 2D array.

30:00

Convert 1D Array Into 2D Array
easy
Free written solution
Topics
Companies

You are given a 0-indexed integer array original and two integers m and n.

Build an m x n 2D array from all the values of original, in order: indexes 0 to n - 1 form row 0, indexes n to 2n - 1 form row 1, and so on.

If original cannot fill an m x n array exactly, return an empty 2D array.

Example 1:
Input: {"original":[1,2,3,4],"m":2,"n":2}
Output: [[1,2],[3,4]]
Constraints:
  • 1≤original.length≤5⋅1041 \leq \text{original.length} \leq 5 \cdot 10^4

  • 1≤original[i]≤1051 \leq \text{original}[i] \leq 10^5

  • 1≤m,n≤4⋅1041 \leq m, n \leq 4 \cdot 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"original":[1,2,3,4],"m":2,"n":2}

original is a tape of 4 values. The plan is to cut it every n = 2 values and stack the pieces as rows.

row i = original[i * 2 : (i + 1) * 2]

original (4 values), cut every n = 2

r0

1
2

r1

3
4

result (2 x 2)

0

1

Variables
VariableValue
len(original)4
m2
n2
DepthFunction Call
Stack empty
0/5