We are given 3 numbers a, b and x. We need to output the multiple of x which is closest to a^b.
Note : b can be a negative number
Input : x = 2, a = 4, b = -2 Output : 0 Explanation : a^b = 1/16. Closest multiple of 2 to 1/16 is 0. Input : x = 4, a = 349, b = 1 Output : 348 Explanation :a^b = 349 The closest multiple of 4 to 349 is 348.
1. When b is negative and a is 1, then a ^ b turns out to be 1 and hence, closest multiple of x becomes either x * 0 or x * 1. 2. When b is negative and a is more than 1, then a ^ b turns out to be less than 1 and hence closest multiple of x becomes 0. 3. When b is positive, calculate a ^ b, then let mul = Integer (a^b / x), then closest multiple of x is mul * x or (mul + 1) * x.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Find unit digit of x raised to power y
- Find value of y mod (2 raised to power x)
- Find last five digits of a given five digit number raised to power five
- Check if a number can be expressed as x^y (x raised to power y)
- Print last k digits of a^b (a raised to power b)
- K-th digit in 'a' raised to power 'b'
- Number of digits in 2 raised to power n
- GCD of a number raised to some power and another number
- Minimum removals in a number to be divisible by 10 power raised to K
- Sum of absolute difference of all pairs raised to power K
- Find the multiple of x which is closest to a^b
- Multiple of x closest to n
- Find power of power under mod of a prime
- Sum of each element raised to (prime-1) % prime
- Last digit of a number raised to last digit of N factorial
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Find the closest and smaller tidy number