Imagine you have a collection of matchsticks of different lengths, and you want to arrange them to form a perfect square! ๐Ÿ”ฅ

Given an integer array matchsticks where matchsticks[i] represents the length of the i-th matchstick, your task is to determine whether you can use all the matchsticks to construct exactly one square.

Rules:

  • You must use every single matchstick exactly once
  • You cannot break or cut any matchstick
  • You can connect matchsticks end-to-end to form longer sides
  • All four sides of the square must have equal length

Goal: Return true if it's possible to form a square, false otherwise.

Example: If you have matchsticks [1,1,2,2,2], you can form a square with sides of length 2 each: [1+1], [2], [2], [2] โœ…

Input & Output

example_1.py โ€” Basic Square Formation
$ Input: [1,1,2,2,2]
โ€บ Output: true
๐Ÿ’ก Note: You can form a square with perimeter 8: each side has length 2. Arrange as [1+1], [2], [2], [2] for the four sides.
example_2.py โ€” Impossible Configuration
$ Input: [3,3,3,3,4]
โ€บ Output: false
๐Ÿ’ก Note: Total length is 16, so each side should be 4. But we have four sticks of length 3 and one of length 4, making it impossible to form equal sides.
example_3.py โ€” Perfect Match
$ Input: [5,5,5,5,4,4,4,4,3,3,3,3]
โ€บ Output: true
๐Ÿ’ก Note: Total is 48, each side should be 12. We can arrange as [5+4+3], [5+4+3], [5+4+3], [5+4+3] to form a perfect square.

Visualization

Tap to expand
Building a Square from MatchsticksStep 1: Available Matchsticks533211Total = 15, Target per side = 15/4 = 3.75 (impossible!)Step 2: Better Example [1,1,2,2,2]22211Total = 8, Target per side = 2 โœ“Step 3: Form SquareTop: 1+1 = 2Right: 2Bottom: 2Left: 2Perfect Square!Step 4: Key Optimizations๐Ÿ” Sort descending: Try large matchsticks firstโšก Early pruning: Skip impossible configurations๐ŸŽฏ Smart backtracking: Avoid duplicate equivalent states๐Ÿ’ก Empty side rule: If stick doesn't fit on empty side, skip other empty sides
Understanding the Visualization
1
Calculate Target
Add up all matchstick lengths and divide by 4 - this is your target side length
2
Sort Strategy
Sort matchsticks from longest to shortest - try big pieces first for faster failure detection
3
Backtrack & Assign
Try placing each matchstick on any side that has room, backtrack when stuck
4
Optimize & Prune
Skip equivalent empty sides and impossible configurations to speed up search
Key Takeaway
๐ŸŽฏ Key Insight: Sort matchsticks in descending order and use backtracking with aggressive pruning - this transforms an exponential problem into something manageable by failing fast on impossible configurations!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(4^n)

For each of n matchsticks, we try placing it on any of 4 sides

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Recursion stack depth equals number of matchsticks

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค matchsticks.length โ‰ค 15
  • 1 โ‰ค matchsticks[i] โ‰ค 108
  • Key insight: Total sum must be divisible by 4
  • All matchsticks must be used exactly once
Asked in
Google 85 Amazon 72 Meta 68 Microsoft 45
62.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen