Find the number closest to n and divisible by m - GeeksforGeeks

16 Apr, 2021
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. Time complexity of O(1) is required.Constraints: m != 0Examples:  
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation to find the number closest to n// and divisible by m#include <bits/stdc++.h> using namespace std; // function to find the number closest to n// and divisible by mint closestNumber(int n, int m){    // find the quotient    int q = n / m;         // 1st possible closest number    int n1 = m * q;         // 2nd possible closest number    int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));         // if true, then n1 is the required closest number    if (abs(n - n1) < abs(n - n2))        return n1;         // else n2 is the required closest number       return n2;   } // Driver program to test aboveint 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 implementation to find the number closest to n// and divisible by mpublic class close_to_n_divisible_m {         // function to find the number closest to n    // and divisible by m    static int closestNumber(int n, int m)    {        // find the quotient        int q = n / m;                  // 1st possible closest number        int n1 = m * q;                  // 2nd possible closest number        int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));                  // if true, then n1 is the required closest number        if (Math.abs(n - n1) < Math.abs(n - n2))            return n1;                  // else n2 is the required closest number           return n2;       }          // Driver program to test above    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));    }}// This code is contributed by Sumit Ghosh
# Python 3 implementation to find# the number closest to n # Function to find the number closest# to n and divisible by mdef closestNumber(n, m) :    # Find the quotient    q = int(n / m)         # 1st possible closest number    n1 = m * q         # 2nd possible closest number    if((n * m) > 0) :        n2 = (m * (q + 1))    else :        n2 = (m * (q - 1))         # if true, then n1 is the required closest number    if (abs(n - n1) < abs(n - n2)) :        return n1         # else n2 is the required closest number    return n2          # Driver program to test aboven = 13; m = 4print(closestNumber(n, m)) n = -15; m = 6print(closestNumber(n, m)) n = 0; m = 8print(closestNumber(n, m)) n = 18; m = -7print(closestNumber(n, m)) # This code is contributed by Nikita tiwari.
// C# implementation to find the// number closest to n and divisible by musing System; class GFG {     // function to find the number closest to n    // and divisible by m    static int closestNumber(int n, int m)    {        // find the quotient        int q = n / m;         // 1st possible closest number        int n1 = m * q;         // 2nd possible closest number        int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));         // if true, then n1 is the required closest number        if (Math.Abs(n - n1) < Math.Abs(n - n2))            return n1;         // else n2 is the required closest number        return n2;    }     // Driver program to test above    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));    }} // This code is contributed by Sam007
<?php// PHP implementation to find// the number closest to n and// divisible by m // function to find the number// closest to n and divisible by mfunction closestNumber($n, $m){    // find the quotient    $q = (int) ($n / $m);         // 1st possible closest number    $n1 = $m * $q;         // 2nd possible closest number    $n2 = ($n * $m) > 0 ?        ($m * ($q + 1)) : ($m * ($q - 1));         // if true, then n1 is the    // required closest number    if (abs($n - $n1) < abs($n - $n2))        return $n1;         // else n2 is the required    // closest number    return $n2;} // Driver Code$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"; // This code is contributed by jit_t?>
<script>// Javascript implementation to find// the number closest to n and// divisible by m // function to find the number// closest to n and divisible by mfunction closestNumber(n, m){     // find the quotient    let q = parseInt(n / m);         // 1st possible closest number    let n1 = m * q;         // 2nd possible closest number    let n2 = (n * m) > 0 ?        (m * (q + 1)) : (m * (q - 1));         // if true, then n1 is the    // required closest number    if (Math.abs(n - n1) < Math.abs(n - n2))        return n1;         // else n2 is the required    // closest number    return n2;} // Driver Codelet 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>"); // This code is contributed by gfgking</script>
Output:  
12
-18
0
21
Time Complexity: O(1)This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. 
AbhishekChomal
jit_t
gfgking
divisibility
Microsoft
Mathematical
Microsoft
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
Program to multiply two matrices
Count ways to reach the n'th stair
Fizz Buzz Implementation
Program to convert a given number to words
Check if a number is Palindrome
Find first and last digits of a number
Generate all permutation of a set in Python