
Problem
Solution
Submissions
Power of a Number
Certification: Basic Level
Accuracy: 13.33%
Submissions: 15
Points: 5
Write a C# program to calculate x raised to the power y (x^y). Given a double x and an integer y, compute x^y efficiently. Consider both positive and negative values of x and y.
Example 1
- Input: x = 2.00000, y = 10
- Output: 1024.00000
- Explanation: 2^10 = 1024
Example 2
- Input: x = 2.10000, y = 3
- Output: 9.26100
- Explanation: 2.1^3 = 2.1 × 2.1 × 2.1 = 9.261
Constraints
- -100.0 < x < 100.0
- -2^31 ≤ y ≤ 2^31-1
- -10^4 ≤ x^y ≤ 10^4
- Time Complexity: O(log n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use fast power algorithm (binary exponentiation) to compute the power efficiently.
- Split the calculation into handling the base and the exponent separately.
- For negative exponents, compute the power with the positive exponent and then take the reciprocal.
- Handle edge cases like x=0, y=0 carefully.