Given an integer x and a positive number y, write a function that computes xy under following conditions.
a) Time complexity of the function should be O(Log y)
b) Extra Space is O(1)
Input: x = 3, y = 5 Output: 243 Input: x = 2, y = 5 Output: 32
We strongly recommend that you click here and practice it, before moving on to the solution.
We have discussed recursive O(Log y) solution for power. The recursive solutions are generally not preferred as they require space on call stack and they involve function call overhead.
Following is implementation to compute xy.
Power is 243
This article is contributed by Udit Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Write a function that generates one of 3 numbers according to given probabilities
- Write a function to get the intersection point of two Linked Lists
- Write a program to calculate pow(x,n)
- Space efficient iterative method to Fibonacci number
- Print all the permutation of length L using the elements of an array | Iterative
- Iterative program to generate distinct Permutations of a String
- Program for Picard's iterative method | Computational Mathematics
- Write a program to add two numbers in base 14
- Ways to write N as sum of two or more positive integers | Set-2
- Count of ways to write N as a sum of three numbers
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Write you own Power without using multiplication(*) and division(/) operators
- Write a program to print all permutations of a given string
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Write a Program to Find the Maximum Depth or Height of a Tree
- Minimum value possible of a given function from the given set
- Reverse and Add Function
- Program for Mobius Function | Set 2