Given three integers a, b and x, the task is to get the multiple of x which is closest to ab.
Examples:
Input: a = 5, b = 4, x = 3
Output: 624
54 = 625 and 624 is the multiple of 3 which is closest to 625Input: a = 349, b = 1, x = 4
Output: 348
Approach:
- Calculate ab and store it in a variable say num.
- Then, calculate ⌊num / x⌋ and store it in a variable floor.
- Now the closest element at the left will be closestLeft = x * floor.
- And the closest element on the right will be closestRight = x * (floor + 1).
- Finally, the closest number among them will be min(num – closestLeft, closestRight – num).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long int // Function to return the multiple of x // which is closest to a^b ll getClosest(int a, int b, int x) { ll num = pow(a, b); int floor = num / x; // Closest element on the left ll numOnLeft = x * floor; // Closest element on the right ll numOnRight = x * (floor + 1); // If numOnLeft is closer than numOnRight if ((num - numOnLeft) < (numOnRight - num)) return numOnLeft; // If numOnRight is the closest else return numOnRight; } // Driver code int main() { int a = 349, b = 1, x = 4; cout << getClosest(a, b, x) << endl; return 0; } |
Java
//Java implementation of the approach public class GFG { // Function to return the multiple of x // which is closest to a^b static long getClosest(int a, int b, int x) { long num = (long) Math.pow(a, b); int floor = (int) (num / x); // Closest element on the left long numOnLeft = x * floor; // Closest element on the right long numOnRight = x * (floor + 1); // If numOnLeft is closer than numOnRight if ((num - numOnLeft) < (numOnRight - num)) { return numOnLeft; } // If numOnRight is the closest else { return numOnRight; } } public static void main(String[] args) { int a = 349, b = 1, x = 4; System.out.println(getClosest(a, b, x)); } } |
Python3
# Python3 implementation of the approach # Function to return the multiple of x # which is closest to a^b def getClosest(a, b, x) : num = pow(a, b) floor = num // x # Closest element on the left numOnLeft = x * floor # Closest element on the right numOnRight = x * (floor + 1) # If numOnLeft is closer than numOnRight if ((num - numOnLeft) < (numOnRight - num)): return numOnLeft # If numOnRight is the closest else : return numOnRight # Driver code if __name__ == "__main__" : a, b, x = 349, 1, 4 print(getClosest(a, b, x)) # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; // #define ll long long int class GFG { // Function to return the multiple of x // which is closest to a^b static long getClosest(int a, int b, int x) { int num = (int)Math.Pow(a, b); int floor = (int)(num / x); // Closest element on the left int numOnLeft = (int)(x * floor); // Closest element on the right int numOnRight = (int)(x * (floor + 1)); // If numOnLeft is closer than numOnRight if ((num - numOnLeft) < (numOnRight - num)) return numOnLeft; // If numOnRight is the closest else return numOnRight; } // Driver code public static void Main() { int a = 349, b = 1, x = 4; Console.WriteLine(getClosest(a, b, x)); } } // This code is contributed // by Akanksha Rai |
PHP
<?php // PHP implementation of the above approach // Function to return the multiple of x // which is closest to a^b function getClosest($a, $b, $x) { $num = pow($a, $b); $floor = (int)($num / $x); // Closest element on the left $numOnLeft = $x * $floor; // Closest element on the right $numOnRight = $x * ($floor + 1); // If numOnLeft is closer than numOnRight if (($num - $numOnLeft) < ($numOnRight - $num)) return $numOnLeft; // If numOnRight is the closest else return ceil($numOnRight); } // Driver code $a = 349; $b = 1; $x = 4; echo getClosest($a, $b, $x); // This code is contributed by jit_t ?> |
348
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.
Recommended Posts:
- Find multiple of x closest to or a ^ b (a raised to power b)
- Multiple of x closest to n
- Find number from given list for which value of the function is closest to A
- Find First element in AP which is multiple of given prime
- Find Nth number in a sequence which is not a multiple of a given number
- Find the closest and smaller tidy number
- Find the number closest to n and divisible by m
- Queries to find the future closest date
- Find a triplet in an array whose sum is closest to a given number
- Find closest integer with the same weight
- Find the K closest points to origin using Priority Queue
- Find K Closest Points to the Origin
- Find the greater number closest to N having at most one non-zero digit
- Smallest multiple of 3 which consists of three given non-zero digits
- Smallest N digit number which is a multiple of 5
- Minimum absolute difference of a number and its closest prime
- Closest perfect square and its distance
- Closest sum partition (into two subsets) of numbers from 1 to n
- Bitwise AND of sub-array closest to K
- Minimum number closest to N made up of odd digits only
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

