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 email@example.com. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program to find whether a no is power of two
- Write an Efficient Method to Check if a Number is Multiple of 3
- Program to find parity
- Power Set
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Write you own Power without using multiplication(*) and division(/) operators
- Find Union and Intersection of two unsorted arrays
- Check if a number is multiple of 5 without using / and % operators
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
- Find the largest multiple of 2, 3 and 5
- Program to find amount of water in a given glass