Grokking Microsoft Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Matchsticks to Square (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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.
  • 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.
  • 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+1 for two sides and 2 for the other two sides).

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible