Hi everyone,
Mahdi Shamlou here 🚀
Today I want to share something different from my LeetCode posts.
Recently, I participated in an algorithm & programming competition at Islamic Azad University — and I won! 🎉
The contest had 4 problems, and I want to start with the first one because it taught me an important lesson about thinking before coding.
🧩 Problem 1 — Two Cubes Puzzle
We had to design two cubes with digits on their faces so that we could display every number from 0 to n.
Rules:
- Each cube has 6 faces (6 digits).
- By placing the cubes side-by-side, we must form all numbers from 0 → n.
- If possible → output cube configuration
- If impossible → return
"Impossible"
Important detail:
We cannot rotate 6 to become 9.
So if we need digit 9, it must exist explicitly on a cube.
💡 My Strategy
At first glance, this looks like a brute-force search problem:
- Generate combinations
- Test permutations
- Check all numbers
But I realized something:
👉 The number of valid configurations is extremely limited.
So instead of writing a complex generator, I solved the cube puzzle manually on paper first.
After finding the correct configuration, I simply returned that fixed solution in code.
No brute force.
No heavy computation.
Just logic.
❗ The Impossible Case
Because we need digit 9 explicitly and cannot reuse 6 as 9:
👉 If n > 30, the answer is "Impossible".
30 is the maximum achievable upper bound with valid cube digit placement.
🐍 Python Implementation
Here is the corrected Python version:
def two_cubes_solution(n):
# Impossible case
if n > 30:
return "Impossible"
# Pre-calculated cube faces (solved manually)
cube1 = [1, 2, 3, 4, 5, 6]
cube2 = [0, 1, 2, 7, 8, 9]
return cube1, cube2
# Example usage
n = int(input())
print(two_cubes_solution(n))
🚀 Why This Approach Won
✔ O(1) time
✔ O(1) space
✔ Zero computation overhead
✔ Guaranteed correctness
The key lesson:
Sometimes the smartest algorithm is solving the logic once and letting the code stay simple.
I’ll share the next competition problem soon — that one was much more algorithm-heavy 😄
Mahdi Shamlou | مهدی شاملو


Top comments (0)