Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–˛455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–˛455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–˛455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–˛455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

679. 24 Game

Hard49.6% Acceptance
ArrayMathBacktracking
Asked by:
H
HRT
ProblemSolutions (3)VideosCompanies (2)Notes

Problem Statement

You are given an integer array cards of length 4. You have four cards, each containing a number in the range [1, 9]. You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parentheses '(' and ')' to get the value 24.

You are restricted with the following rules:

  • The division operator '/' represents real division, not integer division.
    • For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12.
  • Every operation done is between two numbers. In particular, we cannot use '-' as a unary operator.
    • For example, if cards = [1, 1, 1, 1], the expression "-1 - 1 - 1 - 1" is not allowed.
  • You cannot concatenate numbers together
    • For example, if cards = [1, 2, 1, 2], the expression "12 + 12" is not valid.

Return true if you can get such expression that evaluates to 24, and false otherwise.

Example 1:

Input: cards = [4,1,8,7]
Output: true
Explanation: (8-4) * (7-1) = 24

Example 2:

Input: cards = [1,2,1,2]
Output: false

Constraints:

  • cards.length == 4
  • 1 <= cards[i] <= 9
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–˛455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
G
Google

Approach

The 24 Game asks whether four given numbers can be combined using arithmetic operations (+, -, *, /) to evaluate to exactly 24. Since the order of operations and number combinations matter, the most effective strategy is backtracking.

Start by treating the numbers as a list of floating‑point values. At each step, choose any two numbers and apply all possible operations between them. Replace the chosen pair with the computed result and recursively continue with the remaining numbers. Because division introduces decimals, comparisons with 24 should allow a small epsilon tolerance.

This process explores different operation orders and number pairings until either a valid expression producing 24 is found or all possibilities are exhausted. The input size is fixed (four numbers), so although the theoretical complexity grows exponentially with combinations, it remains practical for this constraint.

Complexity

ApproachTime ComplexitySpace Complexity
Backtracking with Pairwise OperationsO(n! * 4^n) in general, but constant for n = 4O(n) recursion stack

Video Solution Available

Sahil & Sarra

View all video solutions

Solutions (3)

Recursive Backtracking with Permutations

This approach uses recursion and backtracking to try every possible permutation of the cards array and examines all the possible binary operations on them. The goal is to see if there is any combination that can evaluate to 24.

We recursively solve smaller expressions, trying each of the four operations in each step on each pair of elements. Since we can use parentheses, we essentially try to evaluate each pair first and reduce it to a smaller problem with one less number.

To handle permutations, we can make use of a generator that produces all permutations of the numbers.

Time Complexity: O(1), because even though there are a large number of potential permutations (4! = 24), the constant nature of 4 elements leads to a constant time complexity.
Space Complexity: O(1), as we reuse input space without additional data structures beyond call stack recursion.

PythonJavaScript
1import itertools
2
3def calculate(a, b):
4    return [a + b, a - b, b - a, a * b, a / b if b != 0 else float('inf'), b / a if a != 0 else float('inf')]
5
6def solve(cards):
7    if len(cards) == 1:
8        return abs(cards[0] - 24) < 1e-6
9    for a, b, *rest in itertools.permutations(cards, 4):
10        for result in calculate(a, b):
11            if solve([result] + rest):
12                return True
13    return False
14
15cards = [4, 1, 8, 7]
16print(solve(cards)) # Output: True

Explanation

The Python solution defines a helper function calculate that takes two numbers and returns a list of results from all possible operations on them. We then use permutations of the integers in cards and apply the operations recursively. The function returns True if any combination of operations results in the number 24.

Iterative Solution with State Compression

Instead of using recursion, we can store intermediate results in a set to track states as we iteratively combine cards using operations. This simulates reducing the problem by iteratively solving and eliminating possibilities that cannot possibly lead to 24.

For each unique combination of card values reduced by each operation, continue until the desired result is achieved or possibilities are exhausted.

Time Complexity: O(1), bounded by 4 elements and evaluation paths.
Space Complexity: O(1), not exceeding static set sizes for temporary card space.

1#include <stdio.h>
2#include <stdlib.h>
3

            

Video Solutions

Watch expert explanations and walkthroughs

Stop solving 500+ Leetcode problems

Sahil & Sarra
0:08512,507 views

Asked By Companies

2 companies
H
HRT
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Rotate ImageMedium
Plus OneEasy
Max Points on a LineHard
Evaluate Reverse Polish NotationMedium
More similar problems

Related Topics

ArrayMathBacktracking

Problem Stats

Acceptance Rate49.6%
DifficultyHard
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is 24 Game asked in FAANG interviews?

Yes, variations of the 24 Game appear in coding interviews at major tech companies. Interviewers use it to evaluate a candidate’s understanding of recursion, backtracking, and careful handling of floating‑point operations.

What is the optimal approach for 24 Game?

The most common approach is backtracking. At each step, select two numbers, apply all arithmetic operations, and recursively continue with the new result included in the remaining list. This systematically explores all possible expressions that could produce 24.

What data structure is best for solving 24 Game?

A dynamic list or array is typically used to store the current set of numbers during recursion. Each recursive call removes two numbers, adds the computed result, and continues exploring possibilities.

Why is backtracking suitable for the 24 Game problem?

Backtracking works well because the problem requires exploring many combinations of numbers and operations. By recursively trying each pair and operation, we can prune paths early if they cannot lead to 24.

#
include
<math.h>
4
5
#
define
EPSILON
1e-6
6
7
int
make24
(
int
index
,
double
nums
[
]
)
{
8
if
(
index
==
1
)
return
fabs
(
nums
[
0
]
-
24
)
<
EPSILON
;
9
for
(
int
i
=
0
;
i
<
index
;
i
++
)
{
10
for
(
int
j
=
i
+
1
;
j
<
index
;
j
++
)
{
11
double
a
=
nums
[
i
]
,
b
=
nums
[
j
]
;
12
nums
[
j
]
=
nums
[
index
-
1
]
;
13
nums
[
i
]
=
a
+
b
;
14
if
(
make24
(
index
-
1
,
nums
)
)
return
1
;
15
nums
[
i
]
=
a
-
b
;
16
if
(
make24
(
index
-
1
,
nums
)
)
return
1
;
17
nums
[
i
]
=
b
-
a
;
18
if
(
make24
(
index
-
1
,
nums
)
)
return
1
;
19
nums
[
i
]
=
a
*
b
;
20
if
(
make24
(
index
-
1
,
nums
)
)
return
1
;
21
if
(
fabs
(
b
)
>
EPSILON
)
{
22
nums
[
i
]
=
a
/
b
;
23
if
(
make24
(
index
-
1
,
nums
)
)
return
1
;
24
}
25
if
(
fabs
(
a
)
>
EPSILON
)
{
26
nums
[
i
]
=
b
/
a
;
27
if
(
make24
(
index
-
1
,
nums
)
)
return
1
;
28
}
29
30
nums
[
i
]
=
a
;
31
nums
[
j
]
=
b
;
32
}
33
}
34
return
0
;
35
}
36
37
int
main
(
)
{
38
double
cards
[
]
=
{
4
,
1
,
8
,
7
}
;
39
int
result
=
make24
(
4
,
cards
)
;
40
printf
(
result
?
"true\n"
:
"false\n"
)
;
41
return
0
;
42
}

Explanation

This C solution mimics the recursive logic in a manner akin to iterative through self-contained nested function calls using index-based state tracking. Adjustments are made in-place with result comparisons continuing permutated evaluations of the cards, swapping back after trials.