Skip to main content
Back to problems
Leetcode
Medium
Math
Recursion
Divide and Conquer
Google
Pow(x, n)

Compute xnx^n efficiently for a real base and integer exponent.

Acceptance 80%
Problem Statement

Problem

Given a real number x and a 32-bit signed integer n, return the value of xnx^n.

Your solution should handle positive, negative, and zero exponents. Since n may be negative, the result may be a fraction. Use an approach that is efficient enough for large exponent values.

Input Format

  • A real number x
  • An integer n

Output Format

  • Return the computed value of xnx^n as a real number.

Constraints

  • n fits in a 32-bit signed integer.
  • x is a real number.
  • The answer may be checked with a small floating-point error tolerance.
  • Avoid naive repeated multiplication for large |n|.

Hints

  • Consider how to reduce the number of multiplications by reusing the result of smaller powers.
  • Be careful with negative exponents and with the minimum 32-bit integer value.

Constraints

  • n is a 32-bit signed integer.
  • x is a real number.
  • Floating-point comparisons may allow small error tolerance.
  • Efficient solutions should run in logarithmic time with respect to |n|.
Examples
Sample cases returned by the problem API.

Example 1

Input

x = 2.00000, n = 10

Output

1024.00000

Explanation

210=10242^{10} = 1024.

Example 2

Input

x = 2.10000, n = 3

Output

9.26100

Explanation

2.13=2.1×2.1×2.1=9.2612.1^3 = 2.1 \times 2.1 \times 2.1 = 9.261.

Show 1 more example

Example 3

Input

x = 2.00000, n = -2

Output

0.25000

Explanation

22=1/22=1/4=0.252^{-2} = 1 / 2^2 = 1/4 = 0.25.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.