Product of Remaining Elements

Published by Helen Yu in

Write a function that returns true if you can partition an array into one element and the rest, such that this element is equal to the product of all other elements excluding itself.

Examples

can_partition([2, 8, 4, 1]) ➞ true
# 8 = 2 x 4 x 1

can_partition([-1, -10, 1, -2, 20]) ➞ false

can_partition([-1, -20, 5, -1, -2, 2]) ➞ true

Notes

  • The array may contain duplicates.
  • Multiple solutions can exist, any solution is sufficient to return true.
Watch a quick demo on how Edabit works.