Find the number closest to n and divisible by m
Last Updated :
24 Mar, 2023
Given two integers n and m. The problem is to find the number closest to n and divisible by m. If there are more than one such number, then output the one having maximum absolute value. If n is completely divisible by m, then output n only.
Constraints: m != 0
Examples:
Input : n = 13, m = 4
Output : 12
Input : n = -15, m = 6
Output : -18
Both -12 and -18 are closest to -15, but
-18 has the maximum absolute value.
Source: Microsoft Interview experience | Set 125.
We find value of n/m. Let this value be q. Then we find closest of two possibilities. One is q * m other is (m * (q + 1)) or (m * (q – 1)) depending on whether one of the given two numbers is negative or not.
Algorithm:
closestNumber(n, m)
Declare q, n1, n2
q = n / m
n1 = m * q
if (n * m) > 0
n2 = m * (q + 1)
else
n2 = m * (q - 1)
if abs(n-n1) < abs(n-n2)
return n1
return n2
C++
#include <bits/stdc++.h>
using namespace std;
int closestNumber( int n, int m)
{
int q = n / m;
int n1 = m * q;
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
if ( abs (n - n1) < abs (n - n2))
return n1;
return n2;
}
int main()
{
int n = 13, m = 4;
cout << closestNumber(n, m) << endl;
n = -15; m = 6;
cout << closestNumber(n, m) << endl;
n = 0; m = 8;
cout << closestNumber(n, m) << endl;
n = 18; m = -7;
cout << closestNumber(n, m) << endl;
return 0;
}
|
Java
public class close_to_n_divisible_m {
static int closestNumber( int n, int m)
{
int q = n / m;
int n1 = m * q;
int n2 = (n * m) > 0 ? (m * (q + 1 )) : (m * (q - 1 ));
if (Math.abs(n - n1) < Math.abs(n - n2))
return n1;
return n2;
}
public static void main(String args[])
{
int n = 13 , m = 4 ;
System.out.println(closestNumber(n, m));
n = - 15 ; m = 6 ;
System.out.println(closestNumber(n, m));
n = 0 ; m = 8 ;
System.out.println(closestNumber(n, m));
n = 18 ; m = - 7 ;
System.out.println(closestNumber(n, m));
}
}
|
Python3
def closestNumber(n, m) :
q = int (n / m)
n1 = m * q
if ((n * m) > 0 ) :
n2 = (m * (q + 1 ))
else :
n2 = (m * (q - 1 ))
if ( abs (n - n1) < abs (n - n2)) :
return n1
return n2
n = 13 ; m = 4
print (closestNumber(n, m))
n = - 15 ; m = 6
print (closestNumber(n, m))
n = 0 ; m = 8
print (closestNumber(n, m))
n = 18 ; m = - 7
print (closestNumber(n, m))
|
C#
using System;
class GFG {
static int closestNumber( int n, int m)
{
int q = n / m;
int n1 = m * q;
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
if (Math.Abs(n - n1) < Math.Abs(n - n2))
return n1;
return n2;
}
public static void Main()
{
int n = 13, m = 4;
Console.WriteLine(closestNumber(n, m));
n = -15;
m = 6;
Console.WriteLine(closestNumber(n, m));
n = 0;
m = 8;
Console.WriteLine(closestNumber(n, m));
n = 18;
m = -7;
Console.WriteLine(closestNumber(n, m));
}
}
|
PHP
<?php
function closestNumber( $n , $m )
{
$q = (int) ( $n / $m );
$n1 = $m * $q ;
$n2 = ( $n * $m ) > 0 ?
( $m * ( $q + 1)) : ( $m * ( $q - 1));
if ( abs ( $n - $n1 ) < abs ( $n - $n2 ))
return $n1 ;
return $n2 ;
}
$n = 13;
$m = 4;
echo closestNumber( $n , $m ), "\n" ;
$n = -15;
$m = 6;
echo closestNumber( $n , $m ), "\n" ;
$n = 0;
$m = 8;
echo closestNumber( $n , $m ), "\n" ;
$n = 18;
$m = -7;
echo closestNumber( $n , $m ), "\n" ;
?>
|
Javascript
<script>
function closestNumber(n, m)
{
let q = parseInt(n / m);
let n1 = m * q;
let n2 = (n * m) > 0 ?
(m * (q + 1)) : (m * (q - 1));
if (Math.abs(n - n1) < Math.abs(n - n2))
return n1;
return n2;
}
let n = 13;
let m = 4;
document.write(closestNumber(n, m) + "<br>" );
n = -15;
m = 6;
document.write(closestNumber(n, m) + "<br>" );
n = 0;
m = 8;
document.write(closestNumber(n, m) + "<br>" );
n = 18;
m = -7;
document.write(closestNumber(n, m) + "<br>" );
</script>
|
Output:
12
-18
0
21
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...