Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Matchsticks to Square

Hard

Topics
ArrayBacktrackingBitmaskDynamic Programming

You are given an array nums where nums[i] is the length of the i-th matchstick. You must use every matchstick exactly once, joining them (without breaking any) to form a square. Return true if you can form a square, otherwise false.

Example 1

Input:  matchsticks = [1,1,2,2,2]
Output: true
Explanation: Sides of length 2: {2},{2},{2},{1,1}.

Example 2

Input:  matchsticks = [3,3,3,3,4]
Output: false

Constraints

  • 1 <= nums.length <= 15
  • 1 <= nums[i] <= 10^8
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access