Skip to content
Problem 679 · LeetCode 679(opens in a new tab)

24 Game

HardBacktracking

You hold four cards, cards, each showing a whole number from 1 to 9. Build an arithmetic expression that uses every card exactly once, in any order, with +, −, × and ÷ and any brackets you like.

Division is exact, so fractions are allowed along the way. Every operator joins two values: you cannot put a minus sign in front of a single number, and you cannot glue two cards into one number such as 1 and 2 into 12.

Return true if some expression comes out to exactly 24, and false otherwise.

Example 1

Input:  cards = [5, 1, 3, 1]
Output: true
Why:    (5 + 1) × (3 + 1) = 24
2 more examples and the constraints

Example 2

Input:  cards = [3, 3, 8, 8]
Output: true
Why:    8 ÷ (3 − 8 ÷ 3) = 8 ÷ (1/3) = 24, through a fraction

Example 3

Input:  cards = [1, 1, 1, 2]
Output: false
Why:    the largest you can make is (1 + 1) × (1 + 2) = 6

Constraints

  • cards always has 4 entries
  • 1 ≤ cards[i] ≤ 9

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Every order, operator and bracket shapeO(1) · 7,680 checks
  2. Combine two, recurseO(1)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for one hand of four cardsVerdict
Every order, operator and bracket shapeO(1)
Correct; shapes written by hand
Combine two, recurseO(1)
Best; no shapes, works for any count

The input size is fixed at 4 cards, so both are constant time; work counts the expressions checked.

Edge cases to test

  • A fraction is needed. [3, 3, 8, 8] only works through 8/3, so integer division would answer false.
  • All the same. [6, 6, 6, 6] gives 6 + 6 + 6 + 6; [1, 1, 1, 1] cannot reach 24.
  • Division by zero. [2, 2, 5, 5] can form 2 − 2 = 0; dividing by it must be skipped, not crash.
  • Float error. Results like 23.999999999999996 must count as 24.
  • Order matters. a − b and b − a, a ÷ b and b ÷ a are different results; try both.

Common mistakes

  • Using integer division, which loses the fraction that some hands need.
  • Comparing the final value with == 24 instead of a small tolerance.
  • Trying only a − b and a ÷ b, and forgetting the reversed b − a and b ÷ a.

How to talk through it

  1. 1Clarify. Is division exact or integer? Can I use unary minus or join digits? Must every card be used once?
  2. 2Count the space. 24 orders, 64 operator choices, 5 bracket shapes: 7,680 expressions, so brute force is fine.
  3. 3Simplify the search. Any expression starts by combining two numbers, so pick a pair, replace it with a result, and recurse.
  4. 4Handle floats. Skip division by a value near 0 and compare with 24 using 1e-6.
  5. 5Test out loud. Run [5, 1, 3, 1], [3, 3, 8, 8] which needs a fraction, and [1, 1, 1, 1] which fails.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?