Find multiple of x closest to or a ^ b (a raised to power b)
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
Examples :
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.
Observations :
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.
C++
#include <bits/stdc++.h>
using namespace std;
void multiple( int a, int b, int x)
{
if (b < 0) {
if (a == 1 && x == 1)
cout << "1" ;
else
cout << "0" ;
}
int mul = pow (a, b);
int ans = mul / x;
int ans1 = x * ans;
int ans2 = x * (ans + 1);
cout << (((mul - ans1) <= (ans2 - mul)) ?
ans1 : ans2);
}
int main()
{
int a = 349, b = 1, x = 4;
multiple(a, b, x);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static void multiple( int a, int b, int x)
{
if (b < 0 )
{
if (a == 1 && x == 1 )
System.out.println( "1" );
else
System.out.println( "0" );
}
int mul = ( int )Math.pow(a, b);
int ans = mul / x;
int ans1 = x * ans;
int ans2 = x * (ans + 1 );
System.out.println(((mul - ans1)
<= (ans2 - mul))
? ans1 : ans2);
}
static public void main (String[] args)
{
int a = 349 , b = 1 , x = 4 ;
multiple(a, b, x);
}
}
|
C#
using System;
public class GFG {
static void multiple( int a, int b, int x)
{
if (b < 0) {
if (a == 1 && x == 1)
Console.WriteLine( "1" );
else
Console.WriteLine( "0" );
}
int mul = ( int )Math.Pow(a, b);
int ans = mul / x;
int ans1 = x * ans;
int ans2 = x * (ans + 1);
Console.WriteLine(((mul - ans1)
<= (ans2 - mul))
? ans1 : ans2);
}
static public void Main ()
{
int a = 349, b = 1, x = 4;
multiple(a, b, x);
}
}
|
PHP
<?php
function multiple( $a , $b , $x )
{
if ( $b < 0)
{
if ( $a == 1 && $x == 1)
echo "1" ;
else
echo "0" ;
}
$mul = pow( $a , $b );
$ans = $mul / $x ;
$ans1 = $x * $ans ;
$ans2 = $x * ( $ans + 1);
$k = ((( $mul - $ans1 ) <= ( $ans2 - $mul ))
? $ans1 : $ans2 );
echo ( $k );
}
$a = 348;
$b = 1;
$x = 4;
multiple( $a , $b , $x );
?>
|
Python3
import math
def multiple(a, b, x):
if (b < 0 ):
if (a = = 1 and x = = 1 ):
print ( "1" );
else :
print ( "0" );
mul = int ( pow (a, b));
ans = int (mul / x);
ans1 = x * ans;
ans2 = x * (ans + 1 );
if ((mul - ans1) < = (ans2 - mul)):
print (ans1);
else :
print (ans2);
a = 349 ;
b = 1 ;
x = 4 ;
multiple(a, b, x);
|
Javascript
<script>
function multiple(a , b , x) {
if (b < 0) {
if (a == 1 && x == 1)
document.write( "1" );
else
document.write( "0" );
}
var mul = parseInt( Math.pow(a, b));
var ans = mul / x;
var ans1 = x * ans;
var ans2 = x * (ans + 1);
document.write(((mul - ans1) <= (ans2 - mul)) ? ans1 : ans2);
}
var a = 349, b = 1, x = 4;
multiple(a, b, x);
</script>
|
Output:
348
Time Complexity: O(log b), to find power
Auxiliary Space: O(1),as no extra space is required
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...