title: Wiggle the tower

text:
# Question
Title: Wiggle the tower
Your input is the height map of a tower. The tower is made out of layers, each one being one unit shorter than the one below it. Every layer is completely on top of the previous layer. For example `[1,5,4,3,2]` is a valid input. Here is a visualization:

```
 #
 ##
 ###
 ####
#####

15432

```

Your task is to wiggle the tower. Wiggling works as from top to bottom. For each layer, we first try to shift left. If we can, we shift left and then stop. If we can't, we shift the layer to the right and repeat for the next layer. If we reach the bottom layer we stop. The bottom layer doesn't move anywhere. When a layer is shifted, the layers above it also move the same amount in the same direction.

Here is a wiggle, step by step:

```
 #
 ##
 ###
 ####
#####

  #
 ##
 ###
 ####
#####
Top layer couldn't move left, so move right and repeat

   #
  ##
 ###
 ####
#####
Layer 2 couldn't move left, so move right and repeat

    #
   ##
  ###
 ####
#####
Layer 3 couldn't move left, so move right and repeat

   #
  ##
 ###
####
#####
Layer 4 could move left, so the wiggle stops here.
Wiggling would anyway finish at the second lowest layer, since the lowest layer is immovable.

```

This means that if your program got input `[1,5,4,3,2]` it should return `[2,3,4,5,1]`.

Because of the mathematics of tower wiggling, your input is always a permutation of the positive integers up to the array length, and also there are \\$2^{n-1}\\$ valid inputs (and outputs) for arrays of length \\$n\\$.

# Test cases (all towers up to \\$n=10\\$)

```
[1] -> [1]
[1, 2] -> [2, 1] -> [1, 2]
[1, 2, 3] -> [1, 3, 2] -> [2, 3, 1] -> [3, 2, 1] -> [1, 2, 3]
[1, 2, 3, 4] -> [1, 2, 4, 3] -> [1, 3, 4, 2] -> [1, 4, 3, 2] -> [2, 3, 4, 1] -> [2, 4, 3, 1] -> [3, 4, 2, 1] -> [4, 3, 2, 1] -> [1, 2, 3, 4]
[1, 2, 3, 4, 5] -> [1, 2, 3, 5, 4] -> [1, 2, 4, 5, 3] -> [1, 2, 5, 4, 3] -> [1, 3, 4, 5, 2] -> [1, 3, 5, 4, 2] -> [1, 4, 5, 3, 2] -> [1, 5, 4, 3, 2] -> [2, 3, 4, 5, 1] -> [2, 3, 5, 4, 1] -> [2, 4, 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4, 3, 2, 1] -> [5, 7, 8, 9, 10, 6, 4, 3, 2, 1] -> [5, 7, 8, 10, 9, 6, 4, 3, 2, 1] -> [5, 7, 9, 10, 8, 6, 4, 3, 2, 1] -> [5, 7, 10, 9, 8, 6, 4, 3, 2, 1] -> [5, 8, 9, 10, 7, 6, 4, 3, 2, 1] -> [5, 8, 10, 9, 7, 6, 4, 3, 2, 1] -> [5, 9, 10, 8, 7, 6, 4, 3, 2, 1] -> [5, 10, 9, 8, 7, 6, 4, 3, 2, 1] -> [6, 7, 8, 9, 10, 5, 4, 3, 2, 1] -> [6, 7, 8, 10, 9, 5, 4, 3, 2, 1] -> [6, 7, 9, 10, 8, 5, 4, 3, 2, 1] -> [6, 7, 10, 9, 8, 5, 4, 3, 2, 1] -> [6, 8, 9, 10, 7, 5, 4, 3, 2, 1] -> [6, 8, 10, 9, 7, 5, 4, 3, 2, 1] -> [6, 9, 10, 8, 7, 5, 4, 3, 2, 1] -> [6, 10, 9, 8, 7, 5, 4, 3, 2, 1] -> [7, 8, 9, 10, 6, 5, 4, 3, 2, 1] -> [7, 8, 10, 9, 6, 5, 4, 3, 2, 1] -> [7, 9, 10, 8, 6, 5, 4, 3, 2, 1] -> [7, 10, 9, 8, 6, 5, 4, 3, 2, 1] -> [8, 9, 10, 7, 6, 5, 4, 3, 2, 1] -> [8, 10, 9, 7, 6, 5, 4, 3, 2, 1] -> [9, 10, 8, 7, 6, 5, 4, 3, 2, 1] -> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

```

# Answer
# JavaScript (Node.js), ~~126~~ 121 bytes

-5 byte from Neil

```
f=(x,n=x.length,s=n,j,k=(x+'').match(`(.*?)((\\d+,)?${s})\\b(.*)`))=>k?f(x,--n,[s,n],k):j[1]+j[2].split`,`.reverse()+j[4]

```

Try it online!

Reverse `(_,)n,n-1,n-2,...,n-k` which seems correct but long
> 7 votes

# Answer
# Python NumPy, 57 bytes

```
def f(L):a=L.argmax();m=L>=(a and L[a-1]);L[m]=L[m][::-1]
```

Attempt This Online!

### Old Python NumPy, 59 bytes

```
def f(L):a=L.argmax();m=a and L[a-1];b=L>=m;L[b]=L[b][::-1]
```

Attempt This Online!

Port of @l4m2's JS.

### Stupid Python NumPy, 122 bytes

```
def f(L,i=-2):
 a,*b=L.argsort()[i:];b.sort()
 if[a]<b:L[[a,*b]]=L[[*b,a]]
 else:L[[*b,a]]=L[[a,*b]];-i<len(L)and f(L,i-1)
```

Attempt This Online!

Takes a numpy array and modifies it in-place. Pretty straight-forward algo.
> 4 votes

# Answer
# 05AB1E, 9 bytes

```
Z¡нθO@ÏÂ‡

```

Port of the approach *@l4m2* used in his JavaScript answer, so make sure to upvote him/her as well!

Try it online or verify the test cases up to length 6 (because any more will be too big for TIO's STDOUT).

**Explanation:**

```
          #  e.g. input=[1,3,5,10,9,8,7,6,4,2]
Z         # Get the maximum of the (implicit) input-list
          #  STACK: [1,3,5,10,9,8,7,6,4,2],10
 ¡        # Split the input-list by this maximum
          #  STACK: [[1,3,5],[9,8,7,6,4,2]]
  н       # Leave the first list of the pair (the items before the maximum)
          #  STACK: [1,3,5]
   θ      # Leave the last value of that list (the value just before the maximum)
          #  STACK: 5
    O     # Sum in case the maximum was the very first item, resulting in 0
          #  STACK: 5
     @    # Check for each value in the (implicit) input-list if it's >= this item
          # before the maximum
          #  STACK: [0,0,1,1,1,1,1,1,0,0]
      Ï   # Only leave the items of the (implicit) input-list at the truthy positions
          #  STACK: [5,10,9,8,7,6]
       Â  # Bifurcate this list (short for Duplicate & Reverse copy)
          #  STACK: [5,10,9,8,7,6],[6,7,8,9,10,5]
        ‡ # Transliterate the values in the (implicit) input-list, basically reverting
          # the part that was truthy earlier
          #  STACK: [1,3,6,7,8,9,10,5,4,2]
          # (after which it is output implicitly as result)

```
> 4 votes

# Answer
# , 56 bytes

Or **\>=4.1, 49 bytes** by replacing the word `function` with a `\`.

```
function(v){v[i]=rev(v[i<-v>=sum(v[which.max(v)-1])]);v}

```

Try it online!

Port of @l4m2's answer with the help of @Kevin Cruijssen's explanation.
> 3 votes

# Answer
# 80386 machine code, ~~63~~ 59 bytes

```
\x60\x8d\x1c\x91\x89\xde\x83\xee\x04\x8b\x06\x89\xd1\x29\xc1\x39\xd0\x75\x05
\x48\x01\xd1\xeb\x03\x89\xd0\x41\x89\xf7\xe2\x0e\xa5\x40\x89\x46\xfc\x83\xef
\x08\x39\xf7\x77\xf4\x61\xc3\x83\xc7\x04\x39\xdf\x74\xd2\x39\x07\x75\xce\x48
\xeb\xe2

```

Try it online!

Thanks to @l4m2 suggesting `pusha`/`popa` saving 4 bytes!

**assembly (nasm)**

```
wiggle: ; fastcall, ecx = pointer to array, edx = length of array
    pusha
    lea ebx, [ecx + edx * 4]
    mov esi, ebx
.L0:
    sub esi, 4
    mov eax, [esi]
    mov ecx, edx
    sub ecx, eax
    cmp eax, edx
    jne .0
    dec eax
    add ecx, edx
    jmp .1
.0:
    mov eax, edx
    inc ecx
.1:
    mov edi, esi
.L1:
    loop .2
.L2:
    movsd
    inc eax
    mov [esi - 4], eax
    sub edi, 8
    cmp edi, esi
    ja .L2
    popa
    ret
.2:
    add edi, 4
    cmp edi, ebx
    je .L0
    cmp [edi], eax
    jne .L0
    dec eax
    jmp .L1

```
> 2 votes

# Answer
# JavaScript (Node.js), 299 bytes

```
f=(i,L=0,N=i.map((E,I)=>i.map(G=>I<G|0)).reverse(),Y=Q=>Q.map((e,j)=>Q.reduce((A,g,h)=>A+Q[h][j],0)),F=d=>i[N[L].indexOf(1)-d]&&i[N[L].lastIndexOf(1)-d]&&!(X=N.map((E,I)=>I>L?E:E.map((G,H)=>i[H+d]?E[H+d]:0))).some((E,I)=>E.some((G,H)=>i[I+1]&&G&!X[I+1][H]))&&X)=>F(1)?Y(F(1)):F(-1)?f(Y(F(-1)),L+1):i

```

Try it online!

Yes, I know. Not the shortest. Not anywhere close. However, this answer actually performs the wiggling steps by moving layers left and right.

Builds the list of layers, then attempts to move the current (`L`) layer to the left. If no layer is hovering in midair (some part is above empty air), then we return the resulting arrangement, otherwise we attempt to move to the right. If it is possible, recall `f` with incremented `L`, otherwise, because of the way the wiggling works, we are at the bottom layer and we simply return `i` unchanged.
> 1 votes

# Answer
# Retina, ~~69~~ 68 bytes

```
\d+
*
^|$
,
V`,?(_*),(?=(_+)(?!.*\2))(?<!\2.*)(_+\1,)+
^,|,$

_+
$.&

```

Try it online! Link includes test cases. Edit: Saved 1 byte by using @KevinCruijssen's approach. Explanation:

```
\d+
*
^|$
,

```

Convert the unary and wrap the height map in an extra pair of `,`s.

```
V`,?(_*),(?=(_+)(?!.*\2))(?<!\2.*)(_+\1,)+

```

Get the number preceding the largest number in the input (or `0` if it's the first number), and reverse the sublist of all numbers equal to or exceeding that number. (Excepting `n,n-1,...,1`, valid inputs always contain a sublist of the form `k,n,n-1,...,k+1`.)

```
^,|,$

_+
$.&

```

Remove the extra `,`s and convert to decimal.
> 1 votes

# Answer
# Charcoal, 37 bytes

```
≔⌕θ⌈θη≔⊕⌕θ⊕∧η§θ⊖ηζＩ…θ⊖ηＩ⮌✂θ∧η⊖ηζ¹Ｉ✂θζ

```

Try it online! Link is to verbose version of code. Explanation: Uses @KevinCruijssen's approach.

```
≔⌕θ⌈θη

```

Get the index of the largest element.

```
≔⊕⌕θ⊕∧η§θ⊖ηζ

```

Get the index of the successor of the element one greater than the predecessor of the largest element, or the length of the array (calculated as the index after the position of the `1` which is always last in that case) if the largest element is the first element.

```
Ｉ…θ⊖η

```

Output the elements up to the predecessor of the largest element.

```
Ｉ⮌✂θ∧η⊖ηζ¹

```

Output the elements not less than the predecessor of the largest element, but in reverse.

```
Ｉ✂θζ

```

Output the remaining elements.
> 0 votes

---
Tags: code-golf, array
---


title: Is it a row of Pascal's triangle?

text:
# Question
Title: Is it a row of Pascal's triangle?
Pascal's triangle is a triangular diagram where the values of two numbers added together produce the one below them.

This is the start of it:

```
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

```

You can see that the outside is all 1s, and each number is the sum of the two above it. This continues forever.

Your challenge is to check whether a non-empty array of positive integers is a row of Pascal's triangle, somewhere down the line.

You should output two distinct values for truthy and falsy.

## Scoring

This is , shortest wins.

## Testcases

Truthy:

```
[1]
[1,2,1]
[1,3,3,1]
[1,5,10,10,5,1]
[1,6,15,20,15,6,1]
[1,7,21,35,35,21,7,1]

```

Falsy:

```
[2]
[1,2]
[1,1,2]
[2,2,2,2]
[1,2,10,2,1]
[1,2,3,4,5,6]
[1,3,5,10,5,3,1]

```

# Answer
# Haskell, ~~36~~ ~~31~~ 26 bytes

```
f[1]=1
f(1:b)=f$scanr1(-)b

```

Try it online!

This version errors as a false indicator and returns `1` as the true indicator. It's short but I generally find these sorts of answers a little cheaty so below I have a version with a more traditional output method:

# ~~42~~ ~~37~~ 32 bytes

```
f[1]=1
f(1:b)=f$scanr1(-)b
f _=0

```

Try it online!

Outputs `1` for yes and `0` for no.

This is I believe the only answer here using this method. We use `scanr1(-)` to calculate what the layer above would have to be in order to produce the input, and check if that new smaller layer holds. If we encounter `[1]` we halt with yes, because that is the first layer of Pascal's triangle. And if we encounter something starting with something other than `1` we halt with no.
> 10 votes

# Answer
# Jelly, 6 bytes

```
L’cŻ$⁼

```

Try it online!

As each row of Pascal's triangle has a unique length \\$n\\$, all we have to do is reconstruct the row, given its length, and check if it equals the original input. As each row is given by \\$\binom{n-1}{i}, 0 \le i \< n\\$, we just calculate that (as Jelly has a 1 byte for binomial coefficient)

In fact, if we can take the length of the input as a second input, we can get 5 bytes

## How it works

```
L’cŻ$⁼ - Main link. Takes a list L on the left
L      - Length of L
 ’     - Decrement
    $  - Last 2 links as a monad f(len-1):
   Ż   -   Range from 0 to len-1
  c    -   Binomial coefficient of len-1 and each element in the range
     ⁼ - Does that equal the input?

```
> 9 votes

# Answer
# , 43 bytes

Or **\>=4.1, 36 bytes** by replacing the word `function` by `\`.

```
function(r)any(r-choose(x<-sum(r|1)-1,0:x))

```

Try it online!

Taking also length of input as second argument, we can shave 6 bytes off: Try it online!.
> 7 votes

# Answer
# APL (Dyalog Unicode), ~~11~~ 9 bytes

Saved 2 bytes thanks to Bubbler

```
⊢≡⍳∘≢!≢-≡

```

Try it online!

Requires 0-indexing.

```
⊢≡⍳∘≢!≢-≡
⊢         ⍝ Does the input
 ≡        ⍝ equal
  ⍳∘≢!≢-≡  ⍝ the row of Pascal's triangle with the same length?
     ≢-≡  ⍝ The number r of the real row that has the same length
     ≢    ⍝ The length of the input
      -≡  ⍝ Decremented/Minus the depth (which is always one)
  ⍳∘≢      ⍝ The column numbers for that row (a range [0..r])
    !     ⍝ Get the number at row r and column c for each element in the previous range

```
> 6 votes

# Answer
# C (clang), 66 bytes

```
i,c,t;f(*p,n){for(t=c=i=1;i<=n;c=c*(n-i)/i++)t=t&&p[i-1]==c;*p=t;}

```

Try it online!

# C (clang), 55 bytes

```
i,c;f(*p,n){for(c=i=1;i<=n;c=c*(n-i)/i++)c/=p[i-1]==c;}

```

Inspired by Wheat Wizards' post. Triggers a floating point exception for falsey.

Try it online!
> 6 votes

# Answer
# Lean, ~~187~~ ~~176~~ ~~143~~ 131 bytes

```
def q:list ℕ→bool:=λx,by{induction x,exact[],apply(::)1,induction x_ih with h t i,exact[],cases t,exact[h],exact(h+t_hd)::i}=x

```

Try it online!

A simpler version of this can be found below:

# Lean, ~~190~~ ~~185~~ 169 bytes

```
def f:ℕ→list ℕ:=λn,by{induction n,exact[],apply(::)1,induction n_ih with h t i,exact[],cases t,exact[h],exact(h+t_hd)::i}
def g:list ℕ→bool:=λx,f(x.length)=x

```

Try it online!

This version defines a helper function `f` which gives the \\$n\\$th row of pascal's triangle. From here we check if the input is equal to the \\$m\\$th row where \\$m\\$ is the length of the input.

This is the straightforward way to do things.

The shorter version rolls these two into one. Instead of doing induction on the length of the list, it just does induction on the list itself. This turns the already hairy induction into a real mess.
> 6 votes

# Answer
# Vyxal `r`, 6 bytes

```
₌ẏL‹ƈ⁼

```

Try it Online!

vyxal (and lyxal) is the best?

```
₌ẏL‹ƈ⁼
        (implicit input)
₌       parallel stuffs, do the next two command both on the stack top
 ẏ      range of len(top)
  L     len(top)            currently stack [range(0,len),len]
   ‹    -1                                                len-1
    ƈ                       do combination caculation C(i,len-1) i= range(0,len)
     ⁼                      check equal     stack are not enough so input is used

```
> 5 votes

# Answer
# BQN, 15 13 bytesSBCS

-2 bytes thanks to Marshall Lochbaum

```
-˜`⍟≠≡1‿¯1⥊˜≠

```

Try it here.

Unlike many array languages, BQN doesn't have a binomial primitive, so a more creative solution is required.

```
-˜`⍟≠≡1‿¯1⥊˜≠ #
-˜`           # swapped minus scan
   ⍟≠         # repeated input length number of times
     ≡        # matches
      1‿¯1⥊˜≠ # alternating 1,-1 .. extended to the input length

```
> 5 votes

# Answer
# Julia, ~~47~~ ~~38~~ 34 bytes

```
!l=binomial.((n=[l;0][2];),0:n)==l

```

Try it online!

## Explanation

We just generate the n-th row of the Pascal triangle and test equality.

Thanks to amelies, we can get it down to 38 bytes!

Thanks to MarkMush, we can get it further down to 34 bytes!
> 5 votes

# Answer
# JavaScript (ES6), 46 bytes

Returns a Boolean value.

```
a=>!a.some(v=>v-n|(n*=a.length/q++-1)-n,q=n=1)

```

Try it online!
> 4 votes

# Answer
# Python 3, 66 bytes

```
f=lambda a,*r:r<a[1:]and f(a,*map(sum,zip(r,(1,*r))),1)or(1,*r)==a

```

Try it online!

Constructs the rows of Pascal's triangle recursively until a match is found or the generated row is larger than the input.
> 4 votes

# Answer
# Factor \+ `math.combinatorics`, 47 bytes

```
[ dup length 1 - dup [0,b] [ nCk ] with map = ]

```

Try it online!

## Explanation:

It's a quotation (anonymous function) that takes a sequence of numbers from the data stack as input and leaves a boolean on the data stack as output. Assuming `{ 1 3 3 1 }` is on the data stack when this quotation is called...

* `dup` Duplicate the top object on the data stack.

**Stack:** `{ 1 3 3 1 } { 1 3 3 1 }`
* `length` Get the length of a sequence.

**Stack:** `{ 1 3 3 1 } 4`
* `1 -` Subtract one.

**Stack:** `{ 1 3 3 1 } 3`
* `dup` Duplicate the top object on the data stack.

**Stack:** `{ 1 3 3 1 } 3 3`
* `[0,b]` Create a range from 0 to whatever is on top of the data stack, inclusive.

**Stack:** `{ 1 3 3 1 } 3 T{ range f 0 4 1 }`
* `[ nCk ] with map` Take \\$\binom{3}{0}\\$, \\$\binom{3}{1}\\$, ... \\$\binom{3}{3}\\$

**Stack:** `{ 1 3 3 1 } { 1 3 3 1 }`
* `=` Are the top two objects on the data stack equal?

**Stack:** `t`
> 3 votes

# Answer
# Wolfram Language (Mathematica), 38 bytes

```
Factor@Fold[x#-#+#2&,#]===x^--Tr[1^#]&

```

Try it online!

Checks that the polynomial in \\$(x-1)\\$ with these coefficients is a power of \\$x\\$.
> 3 votes

# Answer
# Python 3.8 (pre-release), 69 bytes

```
lambda l:[math.comb(~-len(l),x)for x in range(len(l))]==l
import math

```

Try it online!

Luckily python 3.8 has a built-in for n choose k
> 3 votes

# Answer
# 05AB1E, 6 bytes

```
g<DÝcQ

```

Port of *@cairdCoinheringaahing*'s Jelly answer.

Try it online or verify all test cases.

**Explanation:**

```
g       # Get the length of the (implicit) input-list
 <      # Decrease it by 1
  D     # Duplicate it
   Ý    # Pop the copy, and push a list in the range [0,length-1]
    c   # Get the binomial coefficient of length-1 with each integer in this list
     Q  # Check if this list is now equal to the (implicit) input-list
        # (after which the result is output implicitly)

```
> 3 votes

# Answer
# Pascal (FPC), 165 bytes

```
function f(a:Array of Integer):integer;var i,n,c:Integer;begin
i:=1;c:=1;f:=1;n:=High(a)+1;for i:=1 to n do begin
if a[i-1]<>c then f:=0;c:=Trunc(c*(n-i)/i);end;end;

```

Try it online!
> 3 votes

# Answer
# Perl 5, ~~45~~ 44 bytes

```
sub{$n=$q=1;!grep$_-$n|($n*=@_/$q++-1)*0,@_}

```

Try it online!

A translation of the JavaScript answer from @Arnauld.
> 2 votes

# Answer
# Pari/GP, 20 bytes

```
a->a==binomial(#a-1)

```

When `binomial` takes only one argument, it returns the `n`'th row of the Pascal's triangle.

Try it online!
> 2 votes

# Answer
# Ruby, 51 bytes

```
->l{l.all?{r=0;*l,r=l.map{|x|r=x-r};r==(l[0]?0:1)}}

```

Try it online!
> 2 votes

# Answer
# Pip `-x`, ~~20~~ 14 bytes

```
L#al+:lPE!ll=a

```

Try it here! TIO doesn't support the `-x` flag, but you can simulate it using the header: Try it online!

### Explanation

We generate the nth row of Pascal's triangle, where n is the length of the input list; then we output truthy if the row and the input are equal, falsey otherwise. Heavily based on my answer to Generate Pascal's triangle, which I recommend you read for a better explanation of the core algorithm.

```
                a is 1st cmdline arg, evaluated (-x flag); l is empty list
L               Loop
 #a              len(a) times:
         !l       1 if l is empty, 0 otherwise
      lPE         l with that value prepended
   l+:            Add to l itemwise and assign back to l
           l=a  1 if l equals a, 0 otherwise

```
> 2 votes

# Answer
# Python 3.8 (pre-release), 71 68 bytes

```
lambda l:[*map(math.comb,(a:=len(l))*[a-1],range(a))]==l
import math

```

Try it online!

Using some list expansion shenanigans, it can be slightly shorter.

Old answer:

```
lambda l:list(map(math.comb,(a:=len(l))*[a-1],range(a)))==l
import math

```

Try it online!

Happily, Python has a built-in combination function. This just uses a simple mapping of that to build the row of Pascal's triangle corresponding to the length of the input list and compares that to the input.
> 2 votes

# Answer
# Desmos, ~~62~~ 48 bytes

*Thanks Bubbler for -14 bytes.*

The function \\$f(l)\\$ takes in a list of numbers, and returns `0` for falsey, `1` for truthy.

```
a=length(l)-1
f(l)=\min(\{l=\nCr(a,[0...a]),0\})

```

Try It On Desmos!

Try It On Desmos! - Prettified
> 1 votes

# Answer
# Charcoal, 20 bytes

```
⬤θ⎇κ⁼×⁻Ｌθκ§θ⊖κ×κι⁼¹ι

```

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. `-` if it's a row, nothing if not. Explanation: The first element must always be `1`. Subsequent elements obey the recurrence relation that the previous element multiplied by the inclusive number of elements remaining equals the current element multiplied by its index.

```
 θ                      Input array
⬤                       Do all elements satisfy
  ⎇κ                    If not first element then
          §θ⊖κ          Previous element
     ×                  Multiplied by
      ⁻Ｌθκ              Number of elements remaining
    ⁼                   Equals
              ×κι       Current element multiplied by its index
                 ⁼¹ι    If first element then it is `1`
                        Implicitly print

```
> 1 votes

---
Tags: code-golf, decision-problem, number-theory
---