2
\$\begingroup\$

Given a non-empty array of positive integers, determine if it is possible to take squares with side lengths specified by each integer, and arrange them in space such that they form a square.

The output can have anything as truthy / falsey values, and the input array may contain duplicates.

For the truthy testcase [3,2,2,2,1,1,1,1], a possible way to arrange the square is:

aaabb
aaabb
aaaef
ccddg
ccddh

where each letter represents a different square. The lengths of the squares are completely specified by the given array.

Numberphile did a video on that which inspired me to write this challenge. He called this an imperfect square. This is so shortest code wins.

Testcases

Truthy:

[1]
[2]
[3]
[3,2,2,2,1,1,1,1]
[12,11,11,7,5,5,4,3,3,2,2,1,1]

Falsy:

[2,1]
[2,2,2]
[3,4]
\$\endgroup\$
9
  • 5
    \$\begingroup\$ Please add test cases. \$\endgroup\$
    – hyper-neutrino
    Jun 7, 2017 at 14:36
  • \$\begingroup\$ This one is about square nor a rectangle, but it's a similar thing. \$\endgroup\$ Jun 7, 2017 at 14:45
  • \$\begingroup\$ I have edited two of your truthy testcases because they were wrong. \$\endgroup\$
    – Leaky Nun
    Jun 7, 2017 at 14:56
  • \$\begingroup\$ Thanks. I worked both of them out by hand and did some mistakes as i typed them in. \$\endgroup\$ Jun 7, 2017 at 14:57
  • 1
    \$\begingroup\$ I thought this was as easy as checking all the ints in the array squared equalled a square number till I saw the [3,4] test case. \$\endgroup\$ Jun 7, 2017 at 15:11

0