Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

MatchStick Problem

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Problem statement

A matchstick puzzle is given below, and you need to make 3 squares of equal size by moving 3 matchsticks. Give all the possible solutions to the puzzle under the given conditions.

1. Breaking of the sticks is not allowed.
2. All the resulting squares must be of equal size.
3. Each stick in the resulting shape must be part of a square.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Solution

Let us discuss a step-by-step solution to achieve the result.

1. Count the number of matchsticks present in the puzzle and make sure they must count to 12, as shown below.

1. We need 4 sticks to form a square. In this way, we need to have 16 sticks to form four squares. But in reality, due to some common sticks like (9, 10, 11, 12), we only need 12 sticks to achieve the goal.
2. After doing this, we have the following goals that we will have to achieve.
• Eliminate all four common sticks.
• Form three independent squares,
• By moving just three sticks.
3. It is interesting to note that moving any common sticks will make it impossible for us to achieve our goal in the remaining two moves. So to achieve our goal, we need to move only the corner sticks, i.e. (1, 2, 3, 4, 5, 6, 7, 8), which are called promising sticks.

Solution 1

1. First, move the stick (2) and place it to the position as depicted in the figure.

1. Move stick numbered 1 to the position as shown in the figure. This results in two free sticks gained, and the two common sticks (9, 10) are no longer common sticks.

1. Now move the stick (6) to the position as depicted in the figure.

To solve the puzzle, one can have other solutions that differ from the solution given above. Let us discuss the other possible solution.

Solution 2

If sticks 7, 8, and 3 are considered for movement instead of 1, 2, and 6 in the first solution.

Solution 3

If sticks 3, 4, 8 are considered for movement instead of 1, 2, 6 in the first solution.

Solution 4

If sticks 1, 2, and 5 are considered for movement instead of 1, 2, and 6 in the first solution.

Check out this problem - 8 Queens Problem

Can we move common sticks in the matchstick problem?

No, we cannot move common sticks because it leads to a situation where we cannot solve the puzzle.

What is the least number of matchsticks required to make 4 square?

We need a minimum of 12 matchsticks required to make 4 squares.

Conclusion

In this article, we have extensively discussed the solution to the matchstick problem with the help of several diagrams. We also discussed all other possible solutions to the matchstick problem.

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
1.
Introduction
2.
Problem statement
3.
Solution
3.1.
Solution 1
3.2.
Solution 2
3.3.
Solution 3
3.4.
Solution 4
4.