Skip to main content
typo fix
Source Link
Neil
  • 177.3k
  • 12
  • 74
  • 281

JavaScript (ES6), 8585 61 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>e==a[j=i&-i]*a[i-j])

Previous version which checked elements to ensure that they were 1 or -1:

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e==a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.
  • The above check gives degenerate results for i not having at least two bits set. In the case of zero bits set, it checks that a[0] == a[0] * a[0], which is false for a[0] == -1, while in the case of one bit set, it checks that a[i] == a[0] * a[i].

JavaScript (ES6), 85 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e==a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.

JavaScript (ES6), 85 61 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>e==a[j=i&-i]*a[i-j])

Previous version which checked elements to ensure that they were 1 or -1:

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e==a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.
  • The above check gives degenerate results for i not having at least two bits set. In the case of zero bits set, it checks that a[0] == a[0] * a[0], which is false for a[0] == -1, while in the case of one bit set, it checks that a[i] == a[0] * a[i].
typo fix
Source Link
Neil
  • 177.3k
  • 12
  • 74
  • 281

JavaScript (ES6), 8485 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e=a[j]*a[ie==a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.

JavaScript (ES6), 84 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e=a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.

JavaScript (ES6), 85 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e==a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.
Source Link
Neil
  • 177.3k
  • 12
  • 74
  • 281

JavaScript (ES6), 84 bytes

a=>(l=a.length)&&!(l&l-1)&a.every((e,i)=>i?(j=i&-i)<i?e=a[j]*a[i-j]:e==1|e==-1:e==1)

Explanation:

  • The length cannot be zero
  • The length must be a power of 2
  • The first element must be 1
  • Elements in positions that are a power of 2 must be either 1 or -1
  • Elements in other positions are the product of all the elements in the positions corresponding to the bitmask, e.g. a[22] == a[2] * a[4] * a[16]. Since a[20] == a[4] * a[16] has already been checked, only a[22] == a[2] * a[20] needs to be checked.