
Matchsticks to Square (medium)
Problem Statement
You are given an integer array matchsticks, where matchsticks[i] represents the length of the i<sup>th</sup> matchstick. You need to make one square by using these all matchsticks. You are not allowed to break any matchstick, but you can link them together, and you can use each matchstick only once.
Return true, if you make one square using all given matchsticks. Otherwise, return false.
Examples
-
Example 1:
- Input:
[5,5,5,5] - Expected Output:
true - Justification: The matchsticks can be arranged to form a square with each side being 5 units long, using all the matchsticks exactly once.
- Input:
-
Example 2:
- Input:
[3,3,3,3,4] - Expected Output:
false - Justification: Despite having four matchsticks of the same length, the additional matchstick of length 4 prevents forming a perfect square.
- Input:
-
Example 3:
- Input:
[1,1,2,1,2,1] - Expected Output:
true - Justification: These matchsticks can be arranged to form a square with each side being 2 units long (
1+1for two sides and2for the other two sides).
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory