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

50. Pow(x, n)

Medium36.0% Acceptance
MathRecursion
Asked by:
A
Asana
eBay
ProblemSolutions (12)VideosCompanies (30)Notes

Problem Statement

Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

Example 1:

Input: x = 2.00000, n = 10
Output: 1024.00000

Example 2:

Input: x = 2.10000, n = 3
Output: 9.26100

Example 3:

Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Constraints:

  • -100.0 < x < 100.0
  • -231 <= n <= 231-1
  • n is an integer.
  • Either x is not zero or n > 0.
  • -104 <= xn <= 104
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
e
G
Goldman Sachs
O
Oracle
P
PayPal
+25

Approach

The Pow(x, n) problem requires computing x^n efficiently, especially when n is very large. A naive approach multiplies x by itself n times, resulting in O(n) time complexity, which becomes inefficient for large inputs.

A better strategy is Exponentiation by Squaring, which reduces the number of multiplications by repeatedly dividing the exponent. If n is even, we compute x^(n/2) once and square the result. If n is odd, we multiply x with x^(n-1). This divide‑and‑conquer technique works naturally with recursion or can be implemented iteratively.

Another important detail is handling negative powers. When n is negative, the result becomes 1 / x^|n|. By halving the exponent at each step, the algorithm runs in O(log n) time, making it significantly faster and suitable for coding interviews.

Complexity

ApproachTime ComplexitySpace Complexity
Naive MultiplicationO(n)O(1)
Exponentiation by Squaring (Recursion/Divide & Conquer)O(log n)O(log n)
Exponentiation by Squaring (Iterative)O(log n)O(1)

Video Solution Available

take U forward

View all video solutions

Solutions (12)

Iterative Method with Exponentiation by Squaring

The iterative method with exponentiation by squaring is an efficient way to calculate powers. It reduces the time complexity by squaring the base and halving the power at each step. This method leverages the mathematical property that xn = (x2)n/2 when n is even and xn = x * xn - 1 when n is odd. By iteratively updating the base and reducing the power, this method achieves a logarithmic time complexity.

Time Complexity: O(log n), Space Complexity: O(1)

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3double myPow(double x, int n) {
4    long long N = n;
5    if (

Explanation

The C solution iteratively calculates the power using a loop. We adjust for negative exponents by taking the reciprocal of x and converting n to positive. This solution uses long long to handle large powers and iteratively squares the base, multiplying it to the result if the current power is odd.

Recursive Divide and Conquer

The recursive divide and conquer method further optimizes power calculation by dividing the problem into smaller subproblems. By recursively dividing the power by 2, this approach minimizes the number of multiplications. If the power is even, it computes (xn/2)2, and if odd, it adjusts with an additional multiplication. This recursive approach can be more intuitive, especially for understanding the problem breakdown.

Time Complexity: O(log n), Space Complexity: O(log n) due to the call stack

CC++JavaPythonC#JavaScript
1#


Video Solutions

Watch expert explanations and walkthroughs

POW(x,n) | Binary Exponentiation | Leetcode

take U forward
8:10273,811 views

Asked By Companies

30 companies
A
Asana
e
eBay
G
Goldman Sachs
O
Oracle
P
PayPal
U
Uber
V
VMware
W
Walmart Labs
H
HSBC
F
Freshworks
Q
Quikr
F
Facebook
P
PayTM
C
CGI Inc.
D
Dell India
Z
Zycus
C
Cadance Design System
C
Careem
A
Amazon
B
Bloomberg
L
LinkedIn
G
Google
M
Microsoft
A
Apple
A
Adobe
Y
Yahoo
T
TikTok
J
JPMorgan and Chase
t
tcs
W
Wix

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

Add Two NumbersMedium
Permutation SequenceHard
Basic CalculatorHard
Power of TwoEasy
More similar problems

Related Topics

MathRecursion

Problem Stats

Acceptance Rate36.0%
DifficultyMedium
Companies30

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Pow(x, n) asked in FAANG interviews?

Yes, this problem is commonly asked in technical interviews because it tests understanding of recursion, divide-and-conquer techniques, and mathematical optimization such as exponentiation by squaring.

What data structure is best for Pow(x, n)?

This problem does not require a special data structure. It mainly relies on mathematical logic and recursion or iteration to repeatedly halve the exponent and compute powers efficiently.

What is the optimal approach for Pow(x, n)?

The optimal approach is Exponentiation by Squaring. It reduces the number of multiplications by dividing the exponent in half at each step, achieving O(log n) time complexity instead of O(n).

How do you handle negative powers in Pow(x, n)?

When the exponent n is negative, the result becomes the reciprocal of the positive power. In practice, you compute x^|n| using the same efficient approach and then return 1 divided by that result.

Previous Problem

Rotate Image

Next Problem

Permutation Sequence

N
<
0
)
{
6
x
=
1
/
x
;
7
N
=
-
N
;
8
}
9
double
result
=
1
;
10
while
(
N
)
{
11
if
(
N
%
2
==
1
)
{
12
result
*=
x
;
13
}
14
x
*=
x
;
15
N
/=
2
;
16
}
17
return
result
;
18
}
19
20
int
main
(
)
{
21
printf
(
"%f\n"
,
myPow
(
2.00000
,
10
)
)
;
22
printf
(
"%f\n"
,
myPow
(
2.10000
,
3
)
)
;
23
printf
(
"%f\n"
,
myPow
(
2.00000
,
-
2
)
)
;
24
return
0
;
25
}
include
<stdio.h>
2
3
double
myPowUtil
(
double
x
,
long
long
n
)
{
4
if
(
n
==
0
)
return
1
;
5
double
half
=
myPowUtil
(
x
,
n
/
2
)
;
6
if
(
n
%
2
==
0
)
{
7
return
half
*
half
;
8
}
else
{
9
return
half
*
half
*
x
;
10
}
11
}
12
13
double
myPow
(
double
x
,
int
n
)
{
14
long
long
N
=
n
;
15
if
(
N
<
0
)
{
16
x
=
1
/
x
;
17
N
=
-
N
;
18
}
19
return
myPowUtil
(
x
,
N
)
;
20
}
21
22
int
main
(
)
{
23
printf
(
"%f\n"
,
myPow
(
2.00000
,
10
)
)
;
24
printf
(
"%f\n"
,
myPow
(
2.10000
,
3
)
)
;
25
printf
(
"%f\n"
,
myPow
(
2.00000
,
-
2
)
)
;
26
return
0
;
27
}

Explanation

The C recursive solution utilizes a helper function to perform the divide and conquer. The base case is when n equals zero, returning 1. Otherwise, it recurs with half the exponent, and then checks if the exponent is odd, multiplying the result by x.