Matchsticks to Square

You are given an integer array matchsticks where matchsticks[i] is the length of the i^th matchstick. You want to use all the matchsticks to make one square. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.

Return true if you can make this square and false otherwise.

Example 1
Inputmatchsticks = [1,1,2,2,2]
Outputtrue
You can form a square with length 2, where one side of the square is made from two sticks with length 1.
Example 2
Inputmatchsticks = [3,3,3,3,4]
Outputfalse
You cannot find a way to form a square with all the matchsticks.

Constraints

  • 1 <= matchsticks.length <= 15
  • 1 <= matchsticks[i] <= 10^8

Asked at 9 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate