Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Calculate a^b Using Exponentiation by Squaring

Basic Acceptance 86.53% Points 10

You are given an integer base a and a non-negative integer exponent b. Your task is to calculate and return a raised to the power b efficiently using exponentiation by squaring.

Exponentiation by squaring reduces the number of multiplications required by repeatedly squaring the base and dividing the exponent by 2. This approach efficiently computes the result without performing b individual multiplications.

Examples
Example 1

Input: a = 2, b = 10

Output: 1024

Explanation: 2 raised to the power 10 is 1024. Exponentiation by squaring computes the result through repeated squaring.

Example 2

Input: a = 3, b = 0

Output: 1

Explanation: Any non-zero number raised to the power 0 is 1.

Example 3

Input: a = 5, b = 3

Output: 125

Explanation: 5 raised to the power 3 is 5 * 5 * 5 = 125.

Constraints
  • -109 <= a <= 109
  • 0 <= b <= 60
Topics
Data Structures Basics Searching Algorithms Sorting Algorithms Recursion Brute Force Algorithm Divide and Conquer Algorithm
Companies
Samsung DE Shaw
Added 05 Nov 2026 86.53% acceptance
Ln 1, Col 1Sign in to save your code
2 10
1024