24 Game - Problem

Imagine you're a mathematician with four number cards in your hand, each showing a digit from 1 to 9. Your challenge? Combine these numbers using basic arithmetic operations (+, -, *, /) and parentheses to create an expression that equals exactly 24!

This is the classic 24 Game - a popular mathematical puzzle that tests your ability to think creatively with numbers. You have complete freedom to:

  • ๐Ÿ“ Rearrange the numbers in any order
  • ๐Ÿงฎ Use any combination of the four basic operations
  • ๐Ÿ”— Group operations with parentheses however you like

Rules to remember:

  • Division represents real division, not integer division (so 4 รท 3 = 1.333...)
  • Every operation must be between two numbers - no unary minus allowed
  • You cannot concatenate digits (no combining 1 and 2 to make 12)
  • You must use all four cards exactly once

Return true if you can create a valid expression that equals 24, false otherwise.

Input & Output

example_1.py โ€” Basic Success Case
$ Input: [4, 1, 8, 7]
โ€บ Output: true
๐Ÿ’ก Note: We can create (8-4) ร— (7-1) = 4 ร— 6 = 24, so this returns true.
example_2.py โ€” Impossible Case
$ Input: [1, 1, 1, 1]
โ€บ Output: false
๐Ÿ’ก Note: No matter how we combine four 1's with basic operations, we cannot reach 24. The maximum we can get is 1+1+1+1=4.
example_3.py โ€” Division Required
$ Input: [1, 2, 1, 2]
โ€บ Output: false
๐Ÿ’ก Note: Even with operations like (1+1) ร— (2+2) = 8 or (1ร—2) + (1ร—2) = 4, we cannot reach 24 with these numbers.

Visualization

Tap to expand
[4,1,8,7]4 numbersPick 4,1Try opsPick 8,7Try opsPick 4,8Try ops4+1=5[5,8,7]4ร—1=4[4,8,7]8+7=15[4,1,15]8-7=1[4,1,1]7-1=6[4,8,6][4,8,7][4,1,1][4,8,6]4ร—6=24SUCCESS!
Understanding the Visualization
1
Root: 4 Numbers
Start with all four cards: [4,1,8,7]
2
Level 1: Choose First Pair
Pick any 2 numbers to combine first (6 ways to choose pairs)
3
Level 2: Apply Operations
For each pair, try +, -, *, / operations (up to 6 operations considering order)
4
Level 3: Recurse with 3 Numbers
Continue with result plus 2 remaining numbers
5
Leaf: Check Result
When 1 number remains, check if it equals 24
Key Takeaway
๐ŸŽฏ Key Insight: With only 4 numbers, we can afford to try every possible combination systematically. The recursive backtracking explores all ~1500 possibilities efficiently, guaranteeing we find a solution if one exists!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Although complex, it's constant time since we always have exactly 4 numbers - roughly O(4! * 4^3) = O(1536) operations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Recursion depth is at most 3 levels, and we use constant additional space

n
2n
โœ“ Linear Space

Constraints

  • cards.length == 4
  • 1 โ‰ค cards[i] โ‰ค 9
  • Precision requirement: Consider two numbers equal if their difference is less than 10-6
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
42.5K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen