Let's say we have a 2-D list, like the following one:

\$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} \\ \color{blue}{5} & \color{blue}{6} & \color{blue}{7} & \color{red}{8} \\ \color{green}{9} & \color{green}{10} & \color{blue}{11} & \color{red}{12} \end{bmatrix} \$

Notice how the top row and the right column is red. For the purposes of this question, let's call this a J-bracket. The 2nd J-bracket is highlighted in blue, and the 3rd J-bracket is highlighted in green. Your challenge is not to find the J-brackets, but given the J-bracket list, you need to return the original 2-D list.

In many cases, there will be more than one 2-D list possible based on the J-brackets. In this case, you can return either of the possible 2-D lists, or you can return a list of the possiblities.

Let's say you are given the list [[1,2,3,4,5,6], [1,2,3,4], [1,2]]. This means the 1st J-bracket is [1,2,3,4,5,6], the 2nd J-bracket is [1,2,3,4], and the 3rd one is [1,2]. There are two different possible matrixes that can be created from these J-brackets:

\$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} \\ \color{blue}{1} & \color{blue}{2} & \color{blue}{3} & \color{red}{5} \\ \color{green}{1} & \color{green}{2} & \color{blue}{4} & \color{red}{6} \end{bmatrix} \$


\$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} \\ \color{blue}{1} & \color{blue}{2} & \color{red}{4} \\ \color{green}{1} & \color{blue}{3} & \color{red}{5} \\ \color{green}{2} & \color{blue}{4} & \color{red}{6} \end{bmatrix} \$

Test cases

[[1,2,3,4,5], [1,2,3], [1]] => [[1,2,3], [1,2,4], [1,3,5]]
[[1,2,3,4,5,6], [1,2,3,4], [1,2]] => [[1,2,3], [1,2,4], [1,3,5], [2,4,6]]
                                  OR [[1,2,3,4], [1,2,3,5], [1,2,4,6]]
[[6,8,9,6,5,8,2],[1,7,4,3,2],[2,3,1]] => [[6,8,9,6,5], [1,7,4,3,8], [2,3,1,2,2]]
                                      OR [[6,8,9], [1,7,6], [2,4,5], [3,3,8], [1,2,2]]
[] => []


✝ The name J-bracket was robbed from this question.

  • \$\begingroup\$ Sandbox \$\endgroup\$
    – naffetS
    Commented Apr 18, 2022 at 2:54

10 Answers 10


APL(Dyalog Unicode), 20 bytes SBCS


Try it on APLgolf!

Nest if simple (if the input is []). This is necessary to deal with the empty case.
/ Reduce the list of J-brackets from right to left with:
{ ... } Takes the current matrix as ⍵ and the new J-bracket as ⍺.
-≢⍵ Number of rows, negated
( ... ) Call the tacit function with this as a right argument.
↑∘⍺ Take that many values from ⍺ (taking a negative amount gets values from the end)
⍵,∘⍪ Append to ⍵ as a column.
↓∘⍺⍪ Drop -≢⍵ values from ⍺, prepend that as a row.  


J, 33 26 bytes


Try it online!

A single reduction, where each iteration adds another "J layer". Consider left and right parts like 1 2 3 4 (...) 8 9:

  • ({.~-@#) From the tail of the left list take as many elements as are in the right list:

    3 4
  • ],. Zip with right list

    8 3
    9 4
  • (}.~-@#), Append the remaining front elements of the left list:

    1 2
    8 3
    9 4

Curry (PAKCS), 60 bytes

f((a++b):c)=a:f c!b

Try it online!


K (ngn/k), 41 40 29 27 26 bytes


Try it online!

Simply aping ovs' APL solution.

  • \$\begingroup\$ Removed a stray byte \$\endgroup\$
    – doug
    Commented Apr 18, 2022 at 13:43
  • \$\begingroup\$ @coltim points out that I can simply recurse on the reversed argument. \$\endgroup\$
    – doug
    Commented Apr 18, 2022 at 14:01
  • \$\begingroup\$ @coltim Found two more. (He's pretty good at this..) \$\endgroup\$
    – doug
    Commented Apr 18, 2022 at 14:06
  • \$\begingroup\$ Stray byte leaked back in. Thanks @PyGamer0. \$\endgroup\$
    – doug
    Commented Apr 18, 2022 at 14:11
  • 2
    \$\begingroup\$ The convention here for giving credit for help is, instead of using comments, to update the original post and add something like "-15 bytes thanks to Coltim" right under the TIO link. \$\endgroup\$
    – Jonah
    Commented Apr 18, 2022 at 15:16

PARI/GP, 60 bytes


Attempt This Online!

Returns the one with height ≥ width, e.g. [[1,2,3,4,5,6], [1,2,3,4], [1,2]] => [[1,2,3], [1,2,4], [1,3,5], [2,4,6]].

PARI/GP, 62 bytes


Attempt This Online!

Returns the one with width ≥ height, e.g. [[1,2,3,4,5,6], [1,2,3,4], [1,2]] => [[1,2,3,4], [1,2,3,5], [1,2,4,6]].

In both case, the formula is \$output[i,j]=input[i+\min(w-i-j+1,0),j-\min(w-i-j+1,0)]\$, where \$w\$ is the width of the output matrix.


Charcoal, 17 bytes


Try it online! Link is to verbose version of code. Outputs in Charcoal's default one-element-per-line format. Explanation: Based on my original answer to Find the J twin which has since been superseded by an alternative approach.


Loop over the J-brackets in reverse order.


Append the end elements of the bracket to the existing rows of the predefined empty array.


Append the remainder of the bracket to the predefined empty array.


Output the reverse of the final array.


JavaScript (ES6), 69 bytes

A reduceRight() with a recursive callback function.


Try it online!


Python, 69 bytes

f=lambda L:L and L[-1:1]or[*zip(*zip(L[0],*f(L[1:])),L[0][-len(L):])]

Attempt This Online!


Jelly, 14 bytes


Try it online! (The TIO comes with a footer that prints the Python repr, to ensure empty arrays are visible; but the bare link works as a function.)

This is very likely to be suboptimal, since I do four (!) reversals of arrays, but I couldn't find a better solution with Jelly's functions. There could also be a shorter way to handle [] than ȯW, and I might be able to drop the @. However, it feels like this challenge really happened to hit some weak points in Jelly, as the core of the solutions is tiny.

  • The first (and the @) gives us a right-associative reduce. There's no single-byte right-associative reduce in Jelly.
  • The @ also allows us to use / to pop stuff from the new J-bracket, since we can only apply monads to the left arg of dyadic chains.
  • The last reverses the order of the output lines. Since things popped from the J-bracket's end go to the bottom, we have build the output backwards. Also, unfortunately, there's no prepend to like , so we have to append the "rest" of the J-bracket to the bottom of the matrix (; would require a W).
  • The U's reverse each input and output line. ȯWṚṭṪṭ¥Ɱ¥@/Ṛ is very close to correct, but unfortunately gets the innermost J-bracket in the wrong order. I'd need to U or W it somehow to make the output correct.
ȯW                ȯr Wrap (replace [] with [[]])
  Ṛ               Ṛeverse order of J-brackets
   U              Upend (reverse) each J-bracket
           /      reduce lines with:
          @         (swap args: left=next J-bracket, right=matrix)
        Ɱ           Ɱap over lines of matrix:
     Ḣ                remove Ḣead (first) of J-bracket
      ;               append that to matrix line
    ṭ               ṭack (append) remaining J-bracket to matrix
            Ṛ     Ṛeverse order of lines
             U    Upend (reverse) each line

R, 84 83 bytes


Try it online!

The unsplit strategy corresponding to this answer.

-1 thanks to pajonk.

  • 1
    \$\begingroup\$ I was waiting for you to post it :) BTW, -1 byte \$\endgroup\$
    – pajonk
    Commented Apr 19, 2022 at 8:54
  • \$\begingroup\$ @pajonk you shouldn't wait. Also this is annoyingly long; perhaps another approach is warranted. \$\endgroup\$
    – Giuseppe
    Commented Apr 19, 2022 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.