Find the multiple of x which is closest to a^b
Last Updated :
29 Dec, 2022
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 625
Input: 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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll getClosest( int a, int b, int x)
{
ll num = pow (a, b);
int floor = num / x;
ll numOnLeft = x * floor ;
ll numOnRight = x * ( floor + 1);
if ((num - numOnLeft) < (numOnRight - num))
return numOnLeft;
else
return numOnRight;
}
int main()
{
int a = 349, b = 1, x = 4;
cout << getClosest(a, b, x) << endl;
return 0;
}
|
Java
public class GFG {
static long getClosest( int a, int b, int x) {
long num = ( long ) Math.pow(a, b);
int floor = ( int ) (num / x);
long numOnLeft = x * floor;
long numOnRight = x * (floor + 1 );
if ((num - numOnLeft) < (numOnRight - num)) {
return numOnLeft;
}
else {
return numOnRight;
}
}
public static void main(String[] args) {
int a = 349 , b = 1 , x = 4 ;
System.out.println(getClosest(a, b, x));
}
}
|
Python3
def getClosest(a, b, x) :
num = pow (a, b)
floor = num / / x
numOnLeft = x * floor
numOnRight = x * (floor + 1 )
if ((num - numOnLeft) <
(numOnRight - num)):
return numOnLeft
else :
return numOnRight
if __name__ = = "__main__" :
a, b, x = 349 , 1 , 4
print (getClosest(a, b, x))
|
C#
using System;
class GFG
{
static long getClosest( int a, int b, int x)
{
int num = ( int )Math.Pow(a, b);
int floor = ( int )(num / x);
int numOnLeft = ( int )(x * floor);
int numOnRight = ( int )(x * (floor + 1));
if ((num - numOnLeft) < (numOnRight - num))
return numOnLeft;
else
return numOnRight;
}
public static void Main()
{
int a = 349, b = 1, x = 4;
Console.WriteLine(getClosest(a, b, x));
}
}
|
PHP
<?php
function getClosest( $a , $b , $x )
{
$num = pow( $a , $b );
$floor = (int)( $num / $x );
$numOnLeft = $x * $floor ;
$numOnRight = $x * ( $floor + 1);
if (( $num - $numOnLeft ) <
( $numOnRight - $num ))
return $numOnLeft ;
else
return ceil ( $numOnRight );
}
$a = 349;
$b = 1;
$x = 4;
echo getClosest( $a , $b , $x );
?>
|
Javascript
<script>
function getClosest( a, b, x)
{
let num = Math.pow(a, b);
let floor = Math.floor(num / x);
let numOnLeft = x * floor;
let numOnRight = x * (floor + 1);
if ((num - numOnLeft) < (numOnRight - num))
return numOnLeft;
else
return numOnRight;
}
let a = 349, b = 1, x = 4;
document.write(getClosest(a, b, x) + "</br>" );
</script>
|
Time Complexity: O(log(b))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...