Algorithm to make numbers from match sticks
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Creating numbers with matchsticks is both a classic puzzle and an intriguing problem-solving exercise. It provides a unique challenge that blends elements of optimization and combinatorial problem-solving. In this article, we will explore the algorithmic approach to generating numbers with matchsticks, breaking down the problem, its constraints, and presenting possible solutions. We'll also delve into some technical explanations and examples for clarity.
Problem Statement
The primary objective is to form numbers using a specified number of matchsticks. Each digit from 0 to 9 uses a different number of matchsticks to be represented on a seven-segment display. The problem is to figure out which numbers can be formed with a given number of matchsticks.
Matchsticks Required for Digits
Here's the breakdown of how many matchsticks are needed to form each digit:
- 0: 6 matchsticks
- 1: 2 matchsticks
- 2: 5 matchsticks
- 3: 5 matchsticks
- 4: 4 matchsticks
- 5: 5 matchsticks
- 6: 6 matchsticks
- 7: 3 matchsticks
- 8: 7 matchsticks
- 9: 6 matchsticks
These counts are based on the assumption of using a standard seven-segment display, typically seen in calculators and digital clocks.
Algorithm Design
The main goal is to determine all possible numbers that can be formed with a specific count of matchsticks. The approach involves leveraging a search algorithm to explore valid combinations of digits.
Steps
- Initialization: Start with the total number of matchsticks available.
- Recursive Search:
- At each step, try placing each digit (0-9) at the current position.
- Subtract the number of matchsticks needed for the chosen digit.
- Recur to the next position with the reduced number of matchsticks.
- Base Case: If the required matchsticks reach zero, a valid number has been formed.
- Optimizations:
- Use memoization to avoid redundant calculations.
- Early pruning by ensuring the remaining number of matchsticks can't form any valid digit.
Pseudocode
- Possible numbers: 18, 81, 117, ...
- Key observation: Some results might form permutations or include leading zeros, which should be handled based on the problem's requirements.
Related reading
- Algorithm to map an interval to a smaller interval
- Algorithm to match pairs from two sets
- Algorithm to merge multiple sorted sequences into one sorted sequence in C
- Algorithm to mix colours on 7 individual pieces of toy
- Algorithm to mix sound
- algorithm to parse string with dictionary
- Algorithm to place a mailbox to minimize the total distance that the residents travel to get their mail
- Algorithm to place the rectangular inside the polygon

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.