hardBacktrackingPure DSA~40 min
Reach 24 From Four Cards
Four drawn cards each show a digit 1–9. Using +, -, *, / and any parenthesization, decide whether they can be combined to make exactly 24 — the classic card game.
Problem
Given an array of 4 integers in the range 1..9, determine if you can reach the value 24 by combining all four using +, -, *, / and parentheses. Each number is used exactly once; division is real (not integer) division. Return true if 24 is achievable, false otherwise.
Input
An array cards of exactly 4 integers, each between 1 and 9.
Output
A boolean: true if 24 is reachable.
Constraints
- cards.length == 4
- 1 <= cards[i] <= 9
- Division is floating-point; compare to 24 within a small epsilon.
Examples
Example 1
Input
cards = [4,1,8,7]
Output
true
(8 - 4) * (7 - 1) = 24.
Example 2
Input
cards = [1,2,1,2]
Output
false
No combination of these four reaches 24.